Flask-migrate: How does the script detect default value of a column set in my model?

Created on 2 Apr 2015  路  2Comments  路  Source: miguelgrinberg/Flask-Migrate

In my model, I add a column like this:
deadline = db.Column(db.Date, default=date.max)

Then the migration script is generated automatically. In upgrade() the operation is like this:
op.add_column('posts', sa.Column('deadline', sa.Date(), nullable=True))

Although no 'default' parameter is assigned in add_column(...) function, I find it works well in my db when a new item is inserted, giving a default date of 9999/12/31.
That it what I want, but how does the script know the default value without any information in migration script?

Most helpful comment

Thanks.

All 2 comments

The default is handled by the model, it is not part of the database schema. SQLAlchemy also supports server-side defaults, which are handled by the database. To use a server-side default use the server_default argument in the column declaration. I haven't checked, but the server-side defaults should be picked up by migration scripts.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings