Flask-migrate: Change Model Attributes And Rename Corresponding Database Columns
I have a bit of experience with Flask but not very much with databases (Flask-migrate / alembic / SqlAlchemy). I'm following this tutorial and things are working quite alright. I
Solution 1:
To simply rename a column in an alembic script (which is the same as flask-migrate), what you do is correct:
op.alter_column('users', 'id', nullable=False, new_column_name='user_id')
The problem comes from, in my opinion, that you need also to change its constraint as primary key:
op.drop_constraint('user_pkey', 'users', type_='primarykey')
op.create_primary_key('user_pkey', 'users', ['user_id'])
You may need to adjust the name of the primary key you re-create depending of your database type (It works like this for me with PostgreSQL)
Autogenerated alembic scripts should always be reviewed, quite often they do not do what we want if it's not for simple changes.
Note: If your column id
was used as a foreign key, you may also want to change the foreign key constraints in other tables.
Alter Primary Key in Alembic describes the same kind of problem.
Post a Comment for "Flask-migrate: Change Model Attributes And Rename Corresponding Database Columns"