【问题标题】:Django 3 Cannot migrate DB syntax error at or near "WITH ORDINALITY"Django 3无法在“WITH ORDINALITY”处或附近迁移数据库语法错误
【发布时间】:2020-08-27 12:26:11
【问题描述】:

我最近将服务器从 Python2.7 升级到 Python3.7,将 Django 从 1.11 升级到 3.0.6。大多数事情都很好,但我无法迁移数据库。关注此链接:Errors when I try to migrate in Django2 我删除了所有迁移,运行了新的 makemigrations,进行了新的 migrate --fake,一切正常。

然后我更新了我的 models.py 文件,它给了我同样的错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/webapps/jboycecom/local/lib/python3.7/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 "/webapps/jboycecom/local/lib/python3.7/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 "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 565, in alter_field
    old_db_params, new_db_params, strict)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/postgresql/schema.py", line 154, in _alter_field
    new_db_params, strict,
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 573, in _alter_field
    fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 1148, in _constraint_names
    constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/postgresql/introspection.py", line 166, in get_constraints
    """, [table_name])
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "WITH ORDINALITY"
LINE 6:                     FROM unnest(c.conkey) WITH ORDINALITY co...

我觉得我在更新 Django 时可能把数据库搞砸了,因为它抱怨我不再使用的非工作 oauth2 导入,但由于我无法迁移数据库,我只是删除了有问题的模型,以及其他所有已编译并且运行良好的东西。

我不确定还可以尝试什么。早期链接的一部分说“截断 _django_migrations_ 表”,但我不知道该怎么做。谷歌没有给我任何帮助。也许这会有所帮助?

【问题讨论】:

    标签: python django


    【解决方案1】:

    好吧,我想不通,所以我只是删除了整个数据库,然后将通过我预先导出的电子表格重新填充。


    实际上,这并没有解决问题。如果我尝试迁移数据库,尝试在 Django 中迁移对 ForeignKey 或 ManyToManyFields 的更改会给我错误。我创建了一个新的 CharField,它迁移得很好。

    我正在运行 PSQL 9.5.21 和 Psycopg 2.8.5,所以这些都不是问题。

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 2021-03-28
      • 2018-06-29
      • 2020-01-03
      • 2018-08-21
      • 2018-01-26
      • 2014-04-26
      • 1970-01-01
      • 2017-02-20
      相关资源
      最近更新 更多