4

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.

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
Neven
  • 453
  • 2
  • 7
  • 14
  • 1
    remove migrations folder in each app. – Ali Aug 18 '18 at 07:07
  • 1
    @LeLouch You are kidding me, right? I know to read. :D That is in utils.py file, which does not file that I created or changed. Something that I have done, probably cause that error in Django automatic created file - utils.py – Neven Aug 18 '18 at 20:08
  • 1
    @MohammadAli Thank you again !!! I couldn't be grateful more! Please, write that as a response and I will accept it. :) – Neven Aug 18 '18 at 20:12
  • If you are using Postgres, check this [answer](https://stackoverflow.com/a/51927443/1556106) – José Feb 26 '19 at 12:17

3 Answers3

9

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

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • Thank you @agro ! I thumbed up your response and confirmed Mohammad because his comment helped me a day ago. But your response is more detailed and I hope it will have so much more thumbs up! – Neven Aug 19 '18 at 19:57
  • You can follow the same steps in production also, just make sure noone else is deploying to prod during that time. The steps mentioned above is a pseudo squash migration procedure. You can always `squash migration` and then do the migration – Arghya Saha Mar 28 '19 at 07:07
5

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.

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
shmakovpn
  • 751
  • 9
  • 10
2

First, you must remove migration folders in each app

then, you can use migration commands

python manage.py makemigrations
python manage.py migrate
Ali
  • 2,541
  • 2
  • 17
  • 31
  • 9
    This would won't work on many corner cases. Kindly avoid deleting migration folder for any migration issue. I used to do the same mistake until I got trapped on this. I'm writing a detailed answer on how to do migration and solve migration issues, which I has helped me till date. – Arghya Saha Aug 19 '18 at 04:33