Django and Azure SQL key error 'deferrable' when start migrate command

Ego*_*gor 14 python django django-models azure-sql-database

I try to connect Django to Azure SQL and have the error KeyError: deferrable when I start to migrate command. I can't find a resolution for this issue.

I use this application:

asgiref==3.2.10
Django==3.1
django-mssql-backend==2.8.1
pyodbc==4.0.30
pytz==2020.1
sqlparse==0.3.1
Run Code Online (Sandbox Code Playgroud)

and this is my config in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'DBNAME',
        'USER': 'DBUSER',
        'PASSWORD': 'PASSWORD',
        'HOST': 'databasename.database.windows.net',
        'PORT': '1433',

        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    },
}
Run Code Online (Sandbox Code Playgroud)

The error is when i try to run 'manage.py migrate`. Everything runs fine until the 8th step. Here's the output:

(venv) C:\Users\...\...\>python manage.py migrate     
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, 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 "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\...\...\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\...\...\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\...\...\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\...\...\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\Users\...\...\lib\site-packages\django\core\management\base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\...\...\lib\site-packages\django\core\management\commands\migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\...\...\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:\Users\...\...\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:\Users\...\...\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\...\...\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:\Users\...\...\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:\Users\...\...\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:\Users\...\...\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:\Users\...\...\lib\site-packages\sql_server\pyodbc\schema.py", line 861, in execute
    sql = str(sql)
  File "C:\Users\...\...\lib\site-packages\django\db\backends\ddl_references.py", line 200, in __str__
    return self.template % self.parts
KeyError: 'deferrable'

(venv) C:\Users\...\...>

Run Code Online (Sandbox Code Playgroud)

but if I connect to DB I see a table created a table from Django.

please help Thanks!

Sat*_*hia 18

django-mssql-backend目前支持到Django 3.0.10。临时修复是将您的 Django 版本从Django 3.1.x降级到 3.0.10

这里提出的错误请求:https : //github.com/ESSolutions/django-mssql-backend/issues/63

  • 截至 2021 年 6 月,版本支持仍然是一个问题。在我从 3.1 降级到 **3.0.14** 之后,这个错误就消失了。 (2认同)

小智 13

我今天遇到了同样的问题。回滚到 Django 3.0.x(我回到 3.0.6),然后运行您的迁移。

我意识到这不是解决方案,但至少它是一种让您前进的方法。迁移后,您应该能够更新回 3.1,它会运行良好。