pypisqlalchemy95% confidence\u2191 89

Alembic: IntegrityError: "column contains null values" when adding non-nullable column

Full error message
I'm adding a column to an existing table. This new column is nullable=False.

op.add_column('mytable', sa.Column('mycolumn', sa.String(), nullable=False))

When I run the migration, it complains:

sqlalchemy.exc.IntegrityError: column "mycolumn" contains null values

It is because your existing data have no value on that new column, i.e. null. Thus causing said error. When adding a non-nullable column, you must decide what value to give to already-existing data Alright, existing data should just have "lorem ipsum" for this new column then. But how do I do it? I can't UPDATE because the column is not there yet. Use the server_default arg: op.add_column('mytable', sa.Column( 'mycolumn', sa.String(), nullable=False, server_default='lorem ipsum', # <--- add this )) But, but, I don't want it to have default value Drop it afterwards using op.alter_column('mytable', 'mycolumn', server_default=None) E.g. your upgrade() function would be: def upgrade(): op.add_column('mytable', sa.Column('mycolumn', sa.String(), nullable=False, server_default='lorem ipsum')) op.alter_column('mytable', 'mycolumn', server_default=None)

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/e8c79c36ed1d3c96719ab0a14cc67a681f3d50b385ac224d153199b0c1cca827
hash \u00b7 e8c79c36ed1d3c96719ab0a14cc67a681f3d50b385ac224d153199b0c1cca827
Alembic: IntegrityError: &quot;column contains null values&q… — DepScope fix | DepScope