3

我有一个 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

有人可以帮忙吗。

4

4 回答 4

4

此问题已在mssql-django. GitHub问题

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

修理:

  1. 安装 mssql-djangopip install mssql-django==1.0rc1
  2. 更改数据库引擎settings.py
DATABASES = {
        'default': {
            'ENGINE': 'mssql',
            ...
        }
    }
于 2021-08-30T14:06:44.920 回答
2

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

我执行以下操作:

  1. 使用pip uninstall django-pyodbcor卸载它pip3 uninstall django-pyodbc(对于 Linux)

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

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

于 2021-06-26T11:55:02.000 回答
0

更改引擎名称对我有用:

# 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/

于 2021-11-26T08:53:53.617 回答
0

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

于 2021-09-22T00:38:54.050 回答