Modifying nullable columns in Alembic
If you try to add a new column to a table and set it to NOT NULL (or alter a column to be NOT NULL), alembic will bork on:
python manage.py db upgrade
It tells you - rightly - that there are (or will be) existing NULL values in the database for that column. The answer is to edit the migration file to update the column before changing the column definition:
from sqlalchemy.sql import table, column
def upgrade():
op.add_column('role', sa.Column('role_name', sa.String(length=30), nullable=True))
role = table('role', column('role_name'))
op.execute(role.update().values(role_name=''))
op.alter_column('role', 'role_name', nullable=False)
Written by Iain Campbell
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#