fix: Correct latest migration alembic/versions/20250414_144046_ebe9468d_update_fruit.py via AI

This commit is contained in:
Backend IM Bot 2025-04-14 14:45:36 +00:00
parent a7c3db994d
commit dab2933d93

View File

@ -13,7 +13,17 @@ branch_labels = None
depends_on = None depends_on = None
def upgrade(): def upgrade():
op.add_column('fruits', sa.Column('shape', sa.String(), nullable=False)) # SQLite doesn't support adding NOT NULL columns without defaults
# We need to use batch operations for this
with op.batch_alter_table('fruits') as batch_op:
batch_op.add_column(sa.Column('shape', sa.String(), nullable=True))
# Update existing rows with a default value
op.execute("UPDATE fruits SET shape = 'unknown' WHERE shape IS NULL")
# Now make the column NOT NULL
batch_op.alter_column('shape',
existing_type=sa.String(),
nullable=False)
def downgrade(): def downgrade():
op.drop_column('fruits', 'shape') with op.batch_alter_table('fruits') as batch_op:
batch_op.drop_column('shape')