I've read through the docs, but I can't find instructions on this anywhere. I tried dropping the old key and adding a new one, but that gets me errors:
op.drop_constraint('PRIMARY', 'some_table', type_='primary')
op.create_primary_key('PRIMARY', 'some_table', ['col1', 'col2'])
sqlalchemy.exc.OperationalError: (OperationalError) (1025, "Error on rename of ... (errno: 150 - Foreign key constraint is incorrectly formed)") 'ALTER TABLE some_table DROP PRIMARY KEY ' ()
What am I doing wrong?
I also was in the same situation: alter primary key. In my case, I had to change the primary key type from integer to string.
The primary key also had a foreign key relationship to another table. The earlier alembic migration created the foreign key constraint in the following way:
Now when changing the primary key type of the
user
table fromInteger
toString
, I had to do the following:Flask version:
0.12.1
Alembic version:
0.9.1
Python version:
3.4.4
Hope this information helps someone facing a similar problem.
I came across this question looking for a sample migration. So here is my full migration that drops the PK constraint and adds a new
AUTO INCREMENT
PK instead:Altering PK on column does not work in alembic, use drop_constraint instead, see here. Hope this helps!