How to solve the djongo.exceptions.SQLDecodeError: while trying to run the migrate command

Question:

I am creating a DjangoRestful app that uses SimpleJWT for authentication. When I try to add the Blacklist app and make migrations. i.e: py manage.py migrate as suggested in the documentation, I get the error below:

Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, main, sessions, token_blacklist
Running migrations:
  Applying token_blacklist.0008_migrate_to_bigautofield...Not implemented alter command for SQL ALTER TABLE "token_blacklist_blacklistedtoken" ALTER COLUMN "id" TYPE long
Traceback (most recent call last):
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongocursor.py", line 51, in execute
    self.result = Query(
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 784, in __init__
    self._query = self.parse()
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 876, in parse
    raise e
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 857, in parse
    return handler(self, statement)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 889, in _alter
    query = AlterQuery(self.db, self.connection_properties, sm, self._params)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 425, in __init__
    super().__init__(*args)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 84, in __init__
    super().__init__(*args)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 62, in __init__
    self.parse()
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 441, in parse
    self._alter(statement)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongosql2mongoquery.py", line 500, in _alter
    raise SQLDecodeError(f'Unknown token: {tok}')
djongo.exceptions.SQLDecodeError:

        Keyword: Unknown token: TYPE
        Sub SQL: None
        FAILED SQL: ('ALTER TABLE "token_blacklist_blacklistedtoken" ALTER COLUMN "id" TYPE long',)
        Params: ([],)
        Version: 1.3.6

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsutils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongocursor.py", line 59, in execute
    raise db_exe from e
djongo.database.DatabaseError

The above exception was the direct cause of the following exception:

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:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangocoremanagement__init__.py", line 425, in execute_from_command_line
    utility.execute()
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangocoremanagement__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangocoremanagementbase.py", line 373, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangocoremanagementbase.py", line 417, in execute
    output = self.handle(*args, **options)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangocoremanagementbase.py", line 90, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangocoremanagementcommandsmigrate.py", line 253, in handle
    post_migrate_state = executor.migrate(
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbmigrationsexecutor.py", line 126, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbmigrationsexecutor.py", line 156, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbmigrationsexecutor.py", line 236, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbmigrationsmigration.py", line 125, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbmigrationsoperationsfields.py", line 225, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsbaseschema.py", line 620, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsbaseschema.py", line 777, in _alter_field
    self.execute(
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsbaseschema.py", line 153, in execute
    cursor.execute(sql, params)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsutils.py", line 99, in execute
    return super().execute(sql, params)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsutils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsutils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsutils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbutils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjangodbbackendsutils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:UsersMR.Robot.virtualenvsfsm-GjGxZg3clibsite-packagesdjongocursor.py", line 59, in execute
    raise db_exe from e
django.db.utils.DatabaseError

I should also mention I am using MongoDB as my database.
Here is my installed apps list:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework_simplejwt.token_blacklist',
    'drf_yasg',
    'main',
    'accounts',
]

When I remove the app from the installed apps and run the command, there is no error.
My package versions:

anyio==3.5.0
asgiref==3.5.0
backports.zoneinfo==0.2.1
certifi==2021.10.8
charset-normalizer==2.0.12
coreapi==2.3.3
coreschema==0.0.4
Django==4.0.2
djangorestframework==3.13.1
djangorestframework-simplejwt==5.0.0
djongo==1.3.6
drf-yasg==1.20.0
idna==3.3
inflection==0.5.1
itypes==1.2.0
Jinja2==3.0.3
MarkupSafe==2.0.1
packaging==21.3
PyJWT==2.3.0
pymongo==3.12.1
pyparsing==3.0.7
pytz==2021.3
requests==2.27.1
ruamel.yaml==0.17.21
ruamel.yaml.clib==0.2.6
sniffio==1.2.0
sqlparse==0.2.4
tk==0.1.0
tzdata==2022.1
uritemplate==4.1.1
urllib3==1.26.8

Does anyone know how to fix this?

Asked By: sajeyks mwangi

||

Answers:

I’ve got the same error and I tried one of solutions suggested here:
https://github.com/doableware/djongo/issues/505#issuecomment-841308612, after which everything looks OK but of course there are probably hidden effects (Django migrate –fake and –fake-initial explained).

So in your case you could try smth. like this:

python manage.py migrate --fake token_blacklist 0008

Update:
I also found out that if I remove all old migrations and create them again from scratch there is no such error. It is again probably NOT a safe approach but it worked for me for my test project as my database is empty anyway:

cd your_app_name/migrations/
rm -rf *
cd ../..
python manage.py makemigrations your_app_name
python manage.py migrate

if python manage.py makemigrations your_app_name does not result in creating an initial migration file, then you can try to initiate first empty migration file with python manage.py makemigrations --empty your_app_name and then do the rest.

Answered By: Villtord

Make AlterField operations no-op.

Add this in manage.py before execute_from_command_line(sys.argv):

if sys.argv[1] == 'migrate':
    from django.conf import settings
    from django.db.migrations import AlterField
    if settings.DATABASES['default']['ENGINE'] == 'djongo':
        AlterField.database_forwards = lambda *_: None
        AlterField.database_backwards = lambda *_: None

References:

Similar issue reported at django migrate is throwing error after configuring mongodb (doableware/djongo#567).

Answered By: aaron