【问题标题】:How to solve the djongo.exceptions.SQLDecodeError: while trying to run the migrate command如何解决 djongo.exceptions.SQLDecodeError:尝试运行 migrate 命令时
【发布时间】:2022-08-15 00:41:34
【问题描述】:

我正在创建一个使用 SimpleJWT 进行身份验证的 DjangoRestful 应用程序。当我尝试添加 Blacklist app 并进行迁移时。即:py manage.py migrate 按照文档中的建议,我收到以下错误:

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:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\cursor.py\", line 51, in execute
    self.result = Query(
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 784, in __init__
    self._query = self.parse()
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 876, in parse
    raise e
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 857, in parse
    return handler(self, statement)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 889, in _alter
    query = AlterQuery(self.db, self.connection_properties, sm, self._params)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 425, in __init__
    super().__init__(*args)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 84, in __init__
    super().__init__(*args)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 62, in __init__
    self.parse()
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.py\", line 441, in parse
    self._alter(statement)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\sql2mongo\\query.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:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\utils.py\", line 85, in _execute
    return self.cursor.execute(sql, params)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\cursor.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:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\core\\management\\__init__.py\", line 425, in execute_from_command_line
    utility.execute()
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\core\\management\\__init__.py\", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\core\\management\\base.py\", line 373, in run_from_argv
    self.execute(*args, **cmd_options)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\core\\management\\base.py\", line 417, in execute
    output = self.handle(*args, **options)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\core\\management\\base.py\", line 90, in wrapped
    res = handle_func(*args, **kwargs)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\core\\management\\commands\\migrate.py\", line 253, in handle
    post_migrate_state = executor.migrate(
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\migrations\\executor.py\", line 126, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\migrations\\executor.py\", line 156, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\migrations\\executor.py\", line 236, in apply_migration
    state = migration.apply(state, schema_editor)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\migrations\\migration.py\", line 125, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\migrations\\operations\\fields.py\", line 225, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\base\\schema.py\", line 620, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\base\\schema.py\", line 777, in _alter_field
    self.execute(
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\base\\schema.py\", line 153, in execute
    cursor.execute(sql, params)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\utils.py\", line 99, in execute
    return super().execute(sql, params)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\utils.py\", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\utils.py\", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\utils.py\", line 85, in _execute
    return self.cursor.execute(sql, params)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\utils.py\", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\django\\db\\backends\\utils.py\", line 85, in _execute
    return self.cursor.execute(sql, params)
  File \"C:\\Users\\MR.Robot\\.virtualenvs\\fsm-GjGxZg3c\\lib\\site-packages\\djongo\\cursor.py\", line 59, in execute
    raise db_exe from e
django.db.utils.DatabaseError

我还应该提到我使用MongoDB 作为我的数据库。 这是我安装的应用程序列表:

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\',
]

当我从已安装的应用程序中删除应用程序并运行命令时,没有错误。 我的包版本:

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

有谁知道如何解决这一问题?

    标签: python django mongodb django-rest-framework django-rest-framework-simplejwt


    【解决方案1】:

    我遇到了同样的错误,我尝试了这里建议的解决方案之一: https://github.com/doableware/djongo/issues/505#issuecomment-841308612,之后一切看起来都不错,但当然可能有隐藏效果(Django migrate --fake and --fake-initial explained)。

    因此,在您的情况下,您可以尝试一下。像这样:

    python manage.py migrate --fake token_blacklist 0008
    

    更新: 我还发现,如果我删除所有旧迁移并从头开始重新创建它们,则不会出现此类错误。这又可能不是一种安全的方法,但它对我的测试项目有用,因为我的数据库无论如何都是空的:

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

    如果python manage.py makemigrations your_app_name 不会导致创建初始迁移文件,那么您可以尝试使用python manage.py makemigrations --empty your_app_name 启动第一个空迁移文件,然后执行其余操作。

    【讨论】:

      【解决方案2】:

      使AlterField 操作无操作。

      execute_from_command_line(sys.argv)之前的manage.py中添加这个:

      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
      

      参考:

      django migrate is throwing error after configuring mongodb (doableware/djongo#567) 报告了类似问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-18
        • 2017-05-31
        • 1970-01-01
        • 1970-01-01
        • 2018-11-05
        • 2021-04-19
        相关资源
        最近更新 更多