SQLAlchemy 2.0 models declared with DeclarativeBase migration not created (on sqlite) by flask-migrate #509
-
Hi, it seems that the env.py generated by flask-migrate isn't able to generate migrations if models are described using the SQLAlchemy 2.0-style (with DeclarativeBase) class. I defined some model (see attached txt) using the new declarative syntax of SQLAlchemy and tried to generate the first migration throught Flask-Migrate but I receive the message "No changes detected". I've tried many solutions without success. Notice that If I put an "old style" (with Column) declaration in my models a create migration is generated. and, below, i change the context.configure that way: ...[CUT]... the first migration is generated. See discussion with the Alembic team here. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm going to copy the answer I put in the Alembic discussion here as well: Just so that we are all on the same page, please keep in mind that Flask-Migrate requires the Flask-SQLAlchemy or Alchemical extensions for Flask, and it provides its own customized template that integrate with these extensions. To work directly with SQLAlchemy as the OP is intending, the env.py file would need to be changed and made to look more like the official Alembic template, but I would suggest using Alembic directly in that case. |
Beta Was this translation helpful? Give feedback.
-
Ok! I understand now. For flask-newbie like me. Models must be subclassed from the Model class of Flask-SQLAlchemy library not from the (generic) SQLAlchemy DeclarativeBase class.
If you need to customize, you have to subclass Model and instanciate SQLAlchemy with the model_class parameter like this:
as documented in the advanced customization. |
Beta Was this translation helpful? Give feedback.
Ok! I understand now.
Thx
A
For flask-newbie like me.
Models must be subclassed from the Model class of Flask-SQLAlchemy library not from the (generic) SQLAlchemy DeclarativeBase class.
In my case db is the Flask-SQLAlchemy object and I have corrected in:
class Layout(db.Model):......
class Routes(db.Model):......
class Nodes(db.Model):......
If you need to customize, you have to subclass Model and instanciate SQLAlchemy with the model_class parameter like this:
class MyBase(Model):......
db = SQLAlchemy(model_class=MyBase):.....
'class Layout(db.Model)'
as documented in the advanced customization.