tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
804 stars 90 forks source link

Changing OneToOneField to ForeignKeyField generates broken SQL #283

Open mo-rijndael opened 1 year ago

mo-rijndael commented 1 year ago

What I done to make it happen

  1. Make model which references other model as One2One. One2One is implemented via foreign + unique
  2. Migrate & apply changes to db
  3. Change field type to ForeignKeyField and migrate

Expected results

Upgrade: alter table ... drop constraint <unique_constrait>; Downgrade: alter table add constraint ...;

Actual results

Upgrade: aerich tries to drop non-existing index (dropping existing one will not work anyway) and then create new foreign key constraint (ignoring already existing) Downgrade: drop duplicated constraint + CREATE UNIQUE INDEX ...

Environment info

I'm using latest aerich from PyPI (0.7.1) and PostgreSQL 15