ESSolutions / django-mssql-backend

Django backend for Microsoft SQL Server
https://pypi.org/project/django-mssql-backend/
BSD 3-Clause "New" or "Revised" License
147 stars 51 forks source link

KeyError: 'include' when executing manage.py sqlmigrate ... #121

Open matthiasguentert opened 3 years ago

matthiasguentert commented 3 years ago

I am pretty new to Django and I am therefore following this official tutorial here. But instead of using Sqlite I'd like to use an MSSQL backend, Azure SQL to be more specific.

However, I am not able to execute python migrate.py migrate nor can I preview the generate SQL statements with python migrate.py sqlmigrate polls 0001.

Interestingly this doesn't happen on django 2.2 LTS (2.2.24) and only happens on 3.2 LTS (3.2.5).

❯ pip list
Package              Version
-------------------- -------
asgiref              3.4.1
Django               3.2.5
django-mssql-backend 2.8.1
pip                  21.1.3
pyodbc               4.0.31
pytz                 2021.1
setuptools           56.0.0
sqlparse             0.4.1

Any idea?

This is what my database configuration looks like in settings.py

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'sqldb-foobar',
        'USER': 'dbadmin@sql-server123',
        'PASSWORD': 'Abracadabra',
        'HOST': 'sql-server123.database.windows.net',
        'PORT': '1433',
        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
            'MARS_Connection': 'True',
        }
    }
}

This is the error that is getting thrown.

❯ python.exe .\manage.py sqlmigrate polls 0001
Traceback (most recent call last):
  File "C:\dev\repos\django\mysite_mssql\manage.py", line 22, in <module>
    main()
  File "C:\dev\repos\django\mysite_mssql\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\commands\sqlmigrate.py", line 29, in execute
    return super().execute(*args, **options)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\commands\sqlmigrate.py", line 65, in handle
    sql_statements = loader.collect_sql(plan)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\loader.py", line 351, in collect_sql
    state = migration.unapply(state, schema_editor, collect_sql=True)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\backends\base\schema.py", line 118, in __exit__
    self.execute(sql)
  File "C:\dev\venv\django3-dev\lib\site-packages\sql_server\pyodbc\schema.py", line 861, in execute
    sql = str(sql)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\backends\ddl_references.py", line 201, in __str__
    return self.template % self.parts
KeyError: 'include'

And the same for the migrate part

❯ python.exe .\manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying contenttypes.0001_initial...Traceback (most recent call last):
  File "C:\dev\repos\django\mysite_mssql\manage.py", line 22, in <module>
    main()
  File "C:\dev\repos\django\mysite_mssql\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\operations\models.py", line 528, in database_forwards
    alter_together(
  File "C:\dev\venv\django3-dev\lib\site-packages\sql_server\pyodbc\schema.py", line 156, in alter_unique_together
    self.execute(sql)
  File "C:\dev\venv\django3-dev\lib\site-packages\sql_server\pyodbc\schema.py", line 861, in execute
    sql = str(sql)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\backends\ddl_references.py", line 201, in __str__
    return self.template % self.parts
KeyError: 'include'

In addition versions 3.1.13, 3.1.12 and 3.1.8 are complaining with a slightly different error

❯ python.exe .\manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length...Traceback (most recent call last):
  File "C:\dev\repos\django\mysite_mssql\manage.py", line 22, in <module>
    main()
  File "C:\dev\repos\django\mysite_mssql\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\core\management\commands\migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\migrations\operations\fields.py", line 236, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\backends\base\schema.py", line 571, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "C:\dev\venv\django3-dev\lib\site-packages\sql_server\pyodbc\schema.py", line 479, in _alter_field
    self.execute(self._create_unique_sql(model, columns=[old_field.column]))
  File "C:\dev\venv\django3-dev\lib\site-packages\sql_server\pyodbc\schema.py", line 861, in execute
    sql = str(sql)
  File "C:\dev\venv\django3-dev\lib\site-packages\django\db\backends\ddl_references.py", line 200, in __str__
    return self.template % self.parts
KeyError: 'deferrable'
YBin1 commented 3 years ago

you can try use 'mssql-django' https://github.com/microsoft/mssql-django

bpsgit commented 3 years ago

Works fine with django 3.0.14. Facing the same issue in django 3.2. Is there a solution for this?

lucasjl commented 7 months ago

Pypi mentions django-mssql-backend supporting up to Django 3.0. For later versions you could try to use a fork of this package named mssql-django (https://github.com/microsoft/mssql-django)