Keyerror 'include' on migrating models to sql server
Asked Answered
E

4

4

I have a django application and have backend as Microsoft sql server. For that, i have installed "pip install django-mssql-backend".

I have extended User model and added one additional field of confirm_password and same i am migrating, but i am getting below error

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'

After this, i can see below two tables created in database

  1. django_migrations
  2. django_content_type

My Model is below:

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

Below is 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 version: 3.2

Can someone help.

Eczema answered 2/5, 2021 at 15:28 Comment(4)
Share the relevant migration that failed.Alephnull
@WillemVanOnsem - Added models.py and forms.py file. Not sure if this is you are asking. I am very new to django, so not up to the mark.Eczema
django-mssql-backend isn't compatible with django 3.2 (at time of writing). Try 3.0, or 3.1 with the mssql-django backend (see: github.com/microsoft/mssql-django/issues/25)Sheen
@TimNyborg - Downgrading django to version 3.0 workedEczema
A
7

This issue is patched in pre-release version 1.0rc1 of mssql-django. GitHub issue

I recommend using mssql-django over django-pyodbc, django-pyodbc-azure-2019 or django-mssql-backend. As it's supported by microsoft and has better support imo.

To fix:

  1. Install mssql-django pip install mssql-django==1.0rc1
  2. Change the database engine in settings.py
DATABASES = {
        'default': {
            'ENGINE': 'mssql',
            ...
        }
    }
Artamas answered 30/8, 2021 at 14:6 Comment(1)
Using mssql-django works for me. There is a collision in django-environ, as it translates schema mssql:// into incorrect engine.Apostle
J
2

Dear I play with this alot and found that issue is django-pyodbc

I do the following:

  1. Uninstall this using pip uninstall django-pyodbc or pip3 uninstall django-pyodbc (for Linux)

  2. Install this using pip install django-pyodbc-azure-2019 or pip3 install django-pyodbc-azure-2019 (for Linux)

My Issue is resolved and hope your app will run smoothly after this. :-)

Josephson answered 26/6, 2021 at 11:55 Comment(1)
It is related with Django version. New version has issues, we need to downgrade. ` pip install django-pyodbc-azure-2019` automatically installs older Django version, that's way how problem is solved.Homeopathist
C
0

Diagnosis :
This is an incompatibility issue between django-mssql-backend and django. django-mssql-backend supports Django 3.0+ only.
Solution :
Rollback to Django 3.0.14 and then run your migrations. It works fine.
Alternatively, you can try this fork supporting 3.2 : https://github.com/microsoft/mssql-django/issues/50

Cook answered 22/9, 2021 at 0:38 Comment(0)
T
0

Changing the Engine name work for me:

# 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", 
        },
    },
}

For more details follow the link: https://learn.microsoft.com/en-us/samples/azure-samples/mssql-django-samples/mssql-django-samples/

Tabby answered 26/11, 2021 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.