Reputation: 163
I have this in my product_api/models.py
from . import db
from datetime import datetime
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True, nullable=False)
slug = db.Column(db.String(255), unique=True, nullable=False)
price = db.Column(db.Integer, nullable=False)
image = db.Column(db.String(255), unique=False, nullable=True)
date_added = db.Column(db.DateTime, default=datetime.utcnow)
date_updated = db.Column(db.DateTime, onupdate=datetime.utcnow)
def to_json(self):
return {
'id': self.id,
'name': self.name,
'slug': self.slug,
'price': self.price,
'image': self.image
}
I run db init and db migrate After the migrate command I get this message UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
However few lines below it says INFO [alembic.autogenerate.compare] Detected added table 'product'
I check MySQL database manually and sure enough, the table is not there, and I cannot reach my API endpoint.
I can see that the migration has created something in the migration file
revision = '66be5d817908'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('product',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('slug', sa.String(length=255), nullable=False),
sa.Column('price', sa.Integer(), nullable=False),
sa.Column('image', sa.String(length=255), nullable=True),
sa.Column('date_added', sa.DateTime(), nullable=True),
sa.Column('date_updated', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name'),
sa.UniqueConstraint('slug')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('product')
Here is my run.py
from application import create_app, db
from application import models
from flask_migrate import Migrate
app = create_app()
migrate = Migrate(app, db)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5002)
I have run through this exact scenario when creating my other micro-service with another database and had no issue whatsoever with migration creating tables. I looked into adding db.create_all()
to my run.py but it didn't really work. I tried dropping the database and starting all over again, that didn't work either. I'm very confused why the first micro-service was working and this one is causing these issues.
EDIT 1 - Here is my config.py
# config.py
import os
from dotenv import load_dotenv
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
load_dotenv(dotenv_path)
class Config:
SQLALCHEMY_TRACK_MODIFICATIONS = False
class DevelopmentConfig(Config):
ENV = "development"
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://kkan@localhost:3306/product';
SQLALCHEMY_ECHO = True
class ProductionConfig(Config):
pass
It looks the same as with my other microservice that works.
Upvotes: 1
Views: 1397
Reputation: 163
Okay, I got it working.
Turns out my CONFIGURATION_SETUP was set to "config.ProductionConfig"
instead of "config.DevelopmentConfig"
Upvotes: 1