当我尝试在Django2中迁移时出现错误

时间:2018-08-17 22:35:26

标签: python django

我有一个问题(至少我认为)。我在这一切上都是新手,所以如果我问一些愚蠢的事情,我深表歉意。 我有一些网站,它正常工作。当我尝试进行迁移(python manage.py makemigrations时,一切顺利,我得到了有关我拥有多少个新模型的信息,等等。但是,当我在进行该迁移后运行时,得到了以下输出:

Operations to perform:
  Apply all migrations: admin, auth, comments, contenttypes, news, sessions
  Running migrations:
   Applying comments.0003_auto_20180816_2158...Traceback (most recent call last):
   File "../venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
  return self.cursor.execute(sql, params)
  psycopg2.ProgrammingError: syntax error at or near "WITH ORDINALITY"
  LINE 6:                     FROM unnest(c.conkey) WITH ORDINALITY co...
                                              ^


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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
   execute_from_command_line(sys.argv)
  File ".../venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
 File ".../venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File ".../venv/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File 
  ".../venv/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
   output = self.handle(*args, **options)
  File 
".../venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
     res = handle_func(*args, **kwargs)
   File ".../venv/lib/python3.6/site- 
 packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
   File 


 ".../venv/lib/python3.6/site- 
packages/django/db/backends/base/schema.py", line 531, in _alter_field
fk_names = self._constraint_names(model, [old_field.column], 
foreign_key=True)
  File ".../venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 1027, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
 File ".../venv/lib/python3.6/site- 
packages/django/db/backends/postgresql/introspection.py", line 158, in get_constraints
""", ["public", table_name])
 File ".../venv/lib/python3.6/site-packages/django/db/backends/utils.py", 
 line 100, in execute
   return super().execute(sql, params)
 File 


".../venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
   return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ".../venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
  File ".../venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
  File "/.../venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
  File ".../venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, 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完成了这项工作,但是当我尝试迁移时,此错误不断出现,我真的很想知道为什么。我试图用谷歌搜索,但是什么也没得到。

3 个答案:

答案 0 :(得分:5)

无论何时添加任何新字段并 遇到迁移错误

请遵循以下步骤:

  • 删除所有迁移文件(而不是迁移文件夹,保留 init.py 文件
  • 截断_django_migrations_表
  • 评论要从 models.py 添加的新字段。 (请确保执行此步骤,否则当您执行以下步骤时django会抛出错误)
  • 运行inputs: dbTierCpu: {default: 2, description: The number of CPUs for the DB node, maximum: 5, minimum: 2, title: DB Server CPU Count, type: integer}
  • 运行python manage.py makemigrations
  • 取消注释要添加的新字段
  • 运行python manage.py migrate --fake
  • 运行python manage.py makemigrations

通常,这些步骤可以解决任何类型的迁移问题。

PS :当您仅在一个应用程序中遇到问题时,我建议从 django_migrations 表中删除该特定应用程序的条目

答案 1 :(得分:0)

首先,您必须在每个应用中删除迁移文件夹

然后,您可以使用迁移命令

auth_permission

答案 2 :(得分:0)

我在默认情况下在Centos 7中安装的postgresql-server-9.2.24-1.el7_5.x86_64遇到了相同的问题。安装Postgresql 11可以解决该问题。