django-pyodbc-azure
django-pyodbc-azure copied to clipboard
Database migration is not created for unique_together field changes
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:
- Create a model (
Model1
) with a foreign key to another model (Model2
). - Add a unique field to to
Model2
(e.g. some natural keyCharField(unique=True)
) - Add
Model2
as a part of aunique_together
field set forModel1
. - Create database migrations:
manage.py makemigrations
- Run the migrations on a MSSQL db:
manage.py migrate
- Remove the
Model2
object from the db, replacing it inunique_together
with some other field. - Create database migrations:
manage.py makemigrations
- 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 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 thanks for the link, but I'm no longer working on that MSSQL project.