Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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',
        },
    },
}

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

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

please help Thanks!

like image 465
Egor Avatar asked Aug 06 '20 11:08

Egor


People also ask

How do I migrate from Django to PostgreSQL on azure?

Django database migrations ensure that the schema in the PostgreSQL on Azure database match those described in your code. Open an SSH session in the browser by navigating to https://<app-name>.scm.azurewebsites.net/webssh/host and sign in with your Azure account credentials (not the database server credentials).

Why does Azure database migration service fail to connect to SQL Server?

This issue happens when the source SQL Server instance that Azure Database Migration Service tries to connect to either has a dynamic port or is using a named instance. The SQL Server Browser service listens to UDP port 1434 for incoming connections to a named instance or when using a dynamic port.

Is there a deferrable keyword argument in Django?

Digging into the code base the problem seems quite straightforward: the django.db.backends.ddl_references.Statement seems indeed to expect a deferrable keyword argument, and after hacking the source code so that an empty string was given as deferrable, the migrations worked just fine.

How do I create a migration in Django?

There are several commands which you will use to interact with migrations and Django’s handling of database schema: migrate, which is responsible for applying and unapplying migrations. makemigrations, which is responsible for creating new migrations based on the changes you have made to your models.


2 Answers

django-mssql-backend currently supporting till Django 3.0.10. The temporary fix is to downgrade your Django version from Django 3.1.x to 3.0.10.

bug request raised here: https://github.com/ESSolutions/django-mssql-backend/issues/63

Edited:

Another solution, use Microsoft's official Django support package to overcome all the issues, the support is very good.

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

like image 57
Sathia Avatar answered Sep 25 '22 17:09

Sathia


I ran in to this same issue today. Roll back to Django 3.0.x (I went back to 3.0.6) and then run your migrations.

I realize that's not a fix, but at least it's a work around to get you going. Once you migrate, you should be able to update back to 3.1 and it will run fine.

like image 41
Dave Clevenger Avatar answered Sep 22 '22 17:09

Dave Clevenger