alembic migrations giving nullable wrongly at times
See original GitHub issueThis seems to be a weird issue.
When I add __versioned__
for the first time to a model and create an alembic migration with:
alembic migrate -m "test"
It gives:
op.create_table('mytable_version',
sa.Column('mycol', sa.BigInteger(), autoincrement=False, nullable=False),
....
)
Then when I run:
alembic upgrade
alembic migrate -m "test"
I was expecting it to say “no changes found”
But it does detect changes and is saying that nullable=True
now:
with op.batch_alter_table('mytable_version', schema=None) as batch_op:
batch_op.alter_column('mycol', existing_type=sa.BigInteger(), nullable=True, autoincrement=False)
This seems to be consistently reproducible - the first time alembic is generating nullable=False
. Then second time alembic is generating nullable=True
Versions I use:
$ venv/bin/pip list | grep -i sqla
Flask-SQLAlchemy 2.5.1
marshmallow-sqlalchemy 0.28.1
SQLAlchemy 1.4.40
SQLAlchemy-Continuum 1.3.12
SQLAlchemy-Utils 0.38.3
sqlalchemy2-stubs 0.0.2a24
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
MSSQL needs "NOT NULL" sent explicitly for a change in type ...
Looks like in alembic when we do an alter_column() to change the type of a column type_= it is ignoring the existing_nullable=True and...
Read more >No changes detected in Alembic autogeneration of migrations ...
Excerpt from the Alembic documentation: Autogenerate will by default detect: Table additions, removals. Column additions, removals. Change of nullable status on ...
Read more >Alembic: How to Add a Non-Nullable Field to a Populated Table
It is quite easy to add a non-nullable field to an empty table. Any migration tool, including Alembic, can automatically generate a migration...
Read more >alter_column Issue (Attempting to Change Column Type)
I'm attempting to use alembic to convert a SQLAlchemy DateTime() field to a SQLAlchemy ... However, running this migration gives me the following...
Read more >Alembic: setting a column to nullable=False if it already has ...
Instead of having to manually make the right changes by hand in the database every time you upgrade your code, you can keep...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Found that this was happenign because of the
col._copy()
function in sqlalchemy After we “copy” the column - we are setting thenullable = True/False
But the value for_user_defined_nullable
is still the previous value from the parent table. So, when we COPY - the parent table’snullable
is used.https://github.com/sqlalchemy/sqlalchemy/blob/rel_1_4_41/lib/sqlalchemy/sql/schema.py#L2018
Adding the
column_copy.nullable
in condition makes sure that if for a non-PK Column nullable is set toFalse
, the versioned col also follow that property.to reproduce the above issue mentioned in thread i created below shown demo model.
and used below mentioned command(s) to generate issue
alembic revision --autogenerate -m "first_commit"
alembic upgrade head
alembic revision --autogenerate -m "without_change_commit"
and after that alembic generated upgrade and downgrade methods post commit
without_change_commit
as shown belowbut after adding that condition to add a condn that non-PK col can also have
nullable=False
when i ran for new sqlite file with new alembic init and then second commitwithout_change_commit
it generated empty upgrade and downgrade methods with no changes detectedi might be wrong also here as later i saw that a testcase is there specific for this behavior.
version i had while trying to replicate the issue