Neven
Neven

Reputation: 453

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.

Upvotes: 4

Views: 11861

Answers (3)

Arghya Saha
Arghya Saha

Reputation: 5733

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 out the new field which is to be added from the models.py. (Do make sure this step otherwise Django will throw an 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 a problem in only one app, I would recommend to delete entries of that particular app from the django_migrations table

If you are looking for solutions which can be applied to production, you can refer to this blog How to fix Django migrations error

Upvotes: 10

shmakovpn
shmakovpn

Reputation: 831

I had the same problem with postgresql-server-9.2.24-1.el7_5.x86_64 that is installing in Centos 7 by default. Installing Postgresql 11 solved the problem.

Upvotes: 5

Ali
Ali

Reputation: 2591

First, you must remove migration folders in each app

then, you can use migration commands

python manage.py makemigrations
python manage.py migrate

Upvotes: 2

Related Questions