Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

python - Errors when I try to migrate in Django2

I have a problem (at least I think). I am new in all this, so I apologize If I ask something stupid. I have some site, which is working normally. When I try to make migrations ( python manage.py makemigrations), everything passed ok, I got the message of how many models new I have, etc. But, when I run after that migrate, I got the following output:

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

Anyway, if after that I try again to make migrations, got the message that I don't have migrations. So, Django did the job, But this error is here constantly when I try to migrate, I am really wondering why. I tried to google it, but I got nothing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Whenever you are adding any new field and facing migrate error.

Kindly follow these steps:

  • Delete all migration files(not the migration folder, keep the __init__.py file
  • Truncate the django_migrations table
  • comment the new field which is to be added from the models.py. (Do make sure this step otherwise django will throw error when you would follow the below steps)
  • run python manage.py makemigrations
  • run python manage.py migrate --fake
  • Uncomment the new field to be added
  • run python manage.py makemigrations
  • run python manage.py migrate

Generally these steps solve any kind of migration problem.

P.S. : When you face problem in only one app, I would recommend to delete entries of that particular app from the django_migrations table


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...