'How to remove default value in model and add alembic version?
I am having a pyramid application which uses sqlalchemy and alembic for databases and migrations. I need to remove default specifier from my model class and add a alembic version script to it.
Previously it was like this:
class TableOne(Base):
__tablename__ = "table_one"
id = Column(Integer, primary_key=True)
field_one = Column(Boolean(name='field_one_bool'), default=False)
I removed the 'default=False' from the field_one variable and tried running:
alembic revision --autogenerate -m "remove default value for field_one"
The alembic version script was generated but all I got inside upgrade() and downgrade() methods were:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
Basically, I need to know what should I do in alembic script if i drop a 'default=False' from the model class.
Solution 1:[1]
The default
of Column
is handled entirely in Python, in contrast with server_default
. Since removing it has no effect on the database, the generated migration script is empty. In other words you do not need a migration in this case.
Solution 2:[2]
If you, like me, wanted to remove server_default
and ended up on this page, the function is alter_column(table_name, column_name, server_default=None)
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ilja Everilä |
Solution 2 | Arno |