How To Solve Django Syntax Error On Migrating


web development

The Django syntax error on migrating is very annoying, specially when you don’t find any error when looking for one. This post contains a working solution.

Table of contents

  • I get a syntax error when trying to migrate in Django
  • The solution

I get a syntax error when trying to migrate in Django

When using the migrate commands in Django

$ python manage.py makemigrations
$ python manage.py migrate

You might get this ugly error:

File "/my/path/lib/python3.11/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers return executor(sql, params, many, context) 
File "/my/path/lib/python3.11/site-packages/django/db/backends/utils.py", line 100, in _execute with self.db.wrap_database_errors: 
File "/my/path/lib/python3.11/site-packages/django/db/utils.py", line 91, in exit_ raise dj_exc_value.with_traceback(traceback) from exc_value 
File "/my/path/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute return self,cursor.execute(sq1,2arams) 
File "/my/path/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py" , line 329, in execute return super().execute(queryparams) 
django.db.utils.OperationalError: near ")": syntax error 

You can confirm using a tool like ruff or copy pasting online in a syntax checker to ensure that there are actually no syntax error. This is troubling.

Apparently, the error is caused by a bad model which got integrated in one of the migrations.

The solution

To solve a syntax error on using the migrate comamnds,

  1. delete all files in all migrations folder except __init__.py
  2. delete the sqlite database if need be

Bit hard, but it solves the issue.

Written by

Abdur-Rahmaan Janhangeer

Chef

Python author of 7+ years having worked for Python companies around the world

Suggested Posts

How To Have Django Packages in Flask

Django packages in Flask seems like a far-away dream, but shopyo 4.6.0 allows you to install Shopyo ...

Read article

Why Choose Flask Over FastAPI

FastAPI positions itself as one of the best choices for API development in Python. The project is po...

Read article

How to run a Flask Linux-only App on Windows - The AFPy Site

There are in the Python world many Flask Linux-only apps. However in many case, with some twerking w...

Read article
Free Flask Course