django-pyodbc-azure icon indicating copy to clipboard operation
django-pyodbc-azure copied to clipboard

Database migration is not created for unique_together field changes

Open lcary opened this issue 6 years ago • 2 comments

It looks like running manage.py makemigrations with this library does not automatically create a migration that removes a unique constraint on a table when the unique_together field changes for a model.

Steps to reproduce:

  1. Create a model (Model1) with a foreign key to another model (Model2).
  2. Add a unique field to to Model2 (e.g. some natural key CharField(unique=True))
  3. Add Model2 as a part of a unique_together field set for Model1.
  4. Create database migrations: manage.py makemigrations
  5. Run the migrations on a MSSQL db: manage.py migrate
  6. Remove the Model2 object from the db, replacing it in unique_together with some other field.
  7. Create database migrations: manage.py makemigrations
  8. Run the latest migrations on the MSSQL db: manage.py migrate

The last step should produce the following error:

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The object '<db>_<model1table>_<model2table>_<model2field>_uniq' is dependent on column '<model2field>'. (5074) (SQLExecDirectW)")

We are currently able to workaround this by creating manual database migrations that look like this:

    migrations.AlterUniqueTogether(
        name='<Model1>',
        unique_together=set(),
    ), 

This wipes the previous constraints. And then we create a followup migration to create the unique_together field. Having to create manual migrations like this is error prone.

See also this ticket where a similar problem was identified and fixed for MySQL: https://code.djangoproject.com/ticket/24757

And for PostgreSQL: https://code.djangoproject.com/ticket/23065 https://code.djangoproject.com/ticket/25648

I think we need a similar fix for MSSQL.

lcary avatar Feb 01 '19 20:02 lcary

@lcary Are you still interested in this feature in MS SQL? I'm currently looking into similar problems as this in the fork https://github.com/ESSolutions/django-mssql-backend and could use some help

OskarPersson avatar Dec 08 '19 19:12 OskarPersson

@OskarPersson thanks for the link, but I'm no longer working on that MSSQL project.

lcary avatar Dec 09 '19 15:12 lcary