python - 启动迁移命令时,Django 和 Azure SQL key 错误 'deferrable'

标签 python django azure django-models azure-sql-database

我尝试将 Django 连接到 Azure SQL,但在开始迁移命令时出现错误 KeyError: deferrable。 我找不到此问题的解决方案。

我使用这个应用程序:

asgiref==3.2.10
Django==3.1
django-mssql-backend==2.8.1
pyodbc==4.0.30
pytz==2020.1
sqlparse==0.3.1

这是我在 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',
        },
    },
}

当我尝试运行“manage.py migrate”时出现错误。一切都运行良好,直到第 8 步。这是输出:

(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\...\...>

但是如果我连接到数据库,我会看到一个表从 Django 创建了一个表。

请帮忙 谢谢!

最佳答案

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

此处提出错误请求:https://github.com/ESSolutions/django-mssql-backend/issues/63

编辑:

另一个解决方案,使用微软官方 Django 支持包来克服所有问题,支持非常好。

https://github.com/microsoft/mssql-django

关于python - 启动迁移命令时,Django 和 Azure SQL key 错误 'deferrable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63282351/

相关文章:

python - 第 1 行带有 print 语句的错误标记

python - 在 matplotlib 2.0 中,如何将颜色条行为恢复为 matplotlib 1.5 的行为?

python - 将元素插入字典列表

django-compressor,heroku,s3 : Request has expired

azure - 无法注册 Azure 集成运行时

azure - 在 Azure 中使用 webapp 日志的正确方法是什么?

python - 字典和字典列表 : conversion and when to use which

python - Django REST - 无法访问我的 API 的编辑页面

python - 如何在 django rest swagger 中隐藏一些 API?

c# - 使用 BrokeredMessage 从 Azure 服务总线队列 (v1) 反序列化强类型对象