michiya / django-pyodbc-azure

Django backend for Microsoft SQL Server and Azure SQL Database using pyodbc
https://pypi.python.org/pypi/django-pyodbc-azure
BSD 3-Clause "New" or "Revised" License
321 stars 140 forks source link

Support unique nullable constraints #144

Closed peakwinter closed 4 years ago

peakwinter commented 6 years ago

This is another attempt at a fix for supporting unique nullable constraints with this library.

Based on work in #43 and resolves issues we encountered with adding/deleting these constraints through Django migrations.

michiya commented 6 years ago

Thank you for the PR but I have found that the change causes the following errors on a Django test below.

Testing against Django installed in 'C:\Users\michiya\workspace\Django-2.0.2\django'
Importing application m2m_through_regress
Creating test database for alias 'default' ('test_defaultdb')...
Operations to perform:
  Synchronize unmigrated apps: auth, contenttypes, m2m_through_regress, messages, sessions, staticfiles
  Apply all migrations: admin, sites
Synchronizing apps without migrations:
  Creating tables...
    Creating table django_content_type
    Creating table auth_permission
    Creating table auth_group
    Creating table auth_user
    Creating table django_session
    Creating table m2m_through_regress_membership
    Creating table m2m_through_regress_usermembership
    Creating table m2m_through_regress_person
    Creating table m2m_through_regress_group
    Creating table m2m_through_regress_a
    Creating table m2m_through_regress_throughbase
    Creating table m2m_through_regress_through
    Creating table m2m_through_regress_b
    Creating table m2m_through_regress_car
    Creating table m2m_through_regress_driver
    Creating table m2m_through_regress_cardriver
    Creating table m2m_through_regress_event
    Creating table m2m_through_regress_competitor
    Creating table m2m_through_regress_individualcompetitor
    Creating table m2m_through_regress_competingteam
    Running deferred SQL...
Traceback (most recent call last):
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\michiya\workspace\django-pyodbc-azure\sql_server\pyodbc\base.py", line 546, in execute
    return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]There are no primary or candidate keys in the referenced table 'm2m_through_regress_car' that match the referencing column list in the foreign key 'm2m_through_regress_cardriver_car_id_02e107b2_fk_m2m_through_regress_car_make'. (1776) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Could not create constraint or index. See previous errors. (1750)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\michiya\workspace\Django-2.0.2\tests\runtests.py", line 481, in <module>
    options.exclude_tags,
  File "C:\Users\michiya\workspace\Django-2.0.2\tests\runtests.py", line 285, in django_tests
    extra_tests=extra_tests,
  File "C:\Users\michiya\workspace\Django-2.0.2\django\test\runner.py", line 601, in run_tests
    old_config = self.setup_databases()
  File "C:\Users\michiya\workspace\Django-2.0.2\django\test\runner.py", line 548, in setup_databases
    self.parallel, **kwargs
  File "C:\Users\michiya\workspace\Django-2.0.2\django\test\utils.py", line 176, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\backends\base\creation.py", line 68, in create_test_db
    run_syncdb=True,
  File "C:\Users\michiya\workspace\Django-2.0.2\django\core\management\__init__.py", line 141, in call_command
    return command.execute(*args, **defaults)
  File "C:\Users\michiya\workspace\Django-2.0.2\django\core\management\base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "C:\Users\michiya\workspace\Django-2.0.2\django\core\management\commands\migrate.py", line 169, in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "C:\Users\michiya\workspace\Django-2.0.2\django\core\management\commands\migrate.py", line 307, in sync_apps
    self.stdout.write("    Running deferred SQL...\n")
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\backends\base\schema.py", line 90, in __exit__
    self.execute(sql)
  File "C:\Users\michiya\workspace\django-pyodbc-azure\sql_server\pyodbc\schema.py", line 659, in execute
    cursor.execute(sql, params)
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\michiya\workspace\Django-2.0.2\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\michiya\workspace\django-pyodbc-azure\sql_server\pyodbc\base.py", line 546, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]There are no primary or candidate keys in the referenced table 'm2m_through_regress_car' that match the referencing column list in the foreign key 'm2m_through_regress_cardriver_car_id_02e107b2_fk_m2m_through_regress_car_make'. (1776) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Could not create constraint or index. See previous errors. (1750)")