Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database migration is not created for unique_together field changes #191

Open
lcary opened this issue Feb 1, 2019 · 2 comments
Open

Database migration is not created for unique_together field changes #191

lcary opened this issue Feb 1, 2019 · 2 comments

Comments

@lcary
Copy link

lcary commented Feb 1, 2019

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.

@OskarPersson
Copy link

OskarPersson commented Dec 8, 2019

@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

@lcary
Copy link
Author

lcary commented Dec 9, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants