【问题标题】:Keyerror 'include' on migrating models to sql server将模型迁移到 sql server 时出现 Keyerror 'include'
【发布时间】:2021-07-25 05:25:03
【问题描述】:

我有一个 django 应用程序,并将后端作为 Microsoft sql server。为此,我安装了“pip install django-mssql-backend”。

我已经扩展了用户模型并添加了一个额外的确认密码字段,我正在迁移,但我遇到了错误

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial...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\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\commands\migrate.py", line 246, in handle
    fake_initial=fake_initial,
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\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\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\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\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\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:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\db\migrations\operations\models.py", line 531, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\sql_server\pyodbc\schema.py", line 156, in alter_unique_together
    self.execute(sql)
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\sql_server\pyodbc\schema.py", line 861, in execute
    sql = str(sql)
  File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\db\backends\ddl_references.py", line 201, in __str__
    return self.template % self.parts
KeyError: 'include'

在这之后,我可以看到下面两个在数据库中创建的表

  1. django_migrations
  2. django_content_type

我的模型如下:

from django.db import models
from django.contrib.auth.models import User

class UserMaster(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)

    # Additional fields
    confirm_password = models.CharField(max_length=10)

    def __str__(self):
        return self.user.username

下面是 Forms.py

from django import forms
from django.contrib.auth.models import User
from retail_forms.models import UserMaster

class UserForm(forms.ModelForm):
    password = forms.CharField(widget = forms.PasswordInput())
    first_name = forms.CharField(required=True)
    last_name = forms.CharField(required=True)

    class Meta():
        model = User
        fields = ('first_name','last_name','email','username','password')

class UserMasterForm(forms.ModelForm):
    confirm_password = forms.CharField(required=True)
    class Meta():
        model = UserMaster
        fields = ('confirm_password',)

Django 版本:3.2

谁能帮忙。

【问题讨论】:

  • 分享失败的相关迁移。
  • @WillemVanOnsem - 添加了 models.py 和 forms.py 文件。不确定这是否是您要问的。我对 django 很陌生,所以还达不到标准。
  • django-mssql-backend 与 django 3.2 不兼容(在撰写本文时)。使用 mssql-django 后端尝试 3.0 或 3.1(请参阅:github.com/microsoft/mssql-django/issues/25
  • @TimNyborg - 将 django 降级到 3.0 版工作

标签: django django-models django-mssql django-mssql-backend


【解决方案1】:

更改 Engine 名称对我有用:

# settings.py
DATABASES = {
    "default": {
        "ENGINE": "mssql",
        "NAME": "DATABASE_NAME",
        "USER": "USER_NAME",
        "PASSWORD": "PASSWORD",
        "HOST": "HOST_ADDRESS",
        "PORT": "1433",
        "OPTIONS": {"driver": "ODBC Driver 17 for SQL Server", 
        },
    },
}

更多详情请点击链接:https://docs.microsoft.com/en-us/samples/azure-samples/mssql-django-samples/mssql-django-samples/

【讨论】:

    【解决方案2】:

    诊断:
    这是 django-mssql-backend 和 django 之间的不兼容问题。 django-mssql-backend 仅支持 Django 3.0+。
    解决方案:
    回滚到 Django 3.0.14,然后运行您的迁移。它工作正常。
    或者,你可以试试这个支持 3.2 的分支:https://github.com/microsoft/mssql-django/issues/50

    【讨论】:

      【解决方案3】:

      亲爱的,我经常玩这个,发现问题是django-pyodbc

      我执行以下操作:

      1. 使用pip uninstall django-pyodbcpip3 uninstall django-pyodbc(适用于Linux)卸载它

      2. 使用pip install django-pyodbc-azure-2019pip3 install django-pyodbc-azure-2019(适用于Linux)安装它

      我的问题已解决,希望您的应用在此之后能够顺利运行。 :-)

      【讨论】:

      • 与Django版本有关。新版本有问题,需要降级。 ` pip install django-pyodbc-azure-2019` 会自动安装较旧的 Django 版本,这就是解决问题的方式。
      【解决方案4】:

      此问题已在mssql-django 的预发布版本 1.0rc1 中修复。 GitHub issue

      我建议使用mssql-django 而不是django-pyodbcdjango-pyodbc-azure-2019django-mssql-backend。因为它得到了微软的支持,并且对 imo 有更好的支持。

      修复:

      1. 安装 mssql-django pip install mssql-django==1.0rc1
      2. 更改settings.py中的数据库引擎
      DATABASES = {
              'default': {
                  'ENGINE': 'mssql',
                  ...
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2018-11-28
        • 2019-09-15
        • 2017-11-08
        • 1970-01-01
        • 2014-05-31
        • 2017-09-07
        • 2018-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多