[Fixed]-Django.db.utils.OperationalError: near "[]": syntax error

28πŸ‘

βœ…

I puzzled over this one for a little while too. The ArrayField is specific to Postgres, and is imported from a Postgres library:

import django.contrib.postgres.fields

It looks like you’re trying to commit your migrations to SQLite. You should set up a local Postgres database, and update your settings.py file from:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

To:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'DATABASE NAME',
        'USER': 'USER NAME',
        'PASSWORD': 'USER PASSWORD',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Also, you are incorrectly setting the default value for your ArrayField. Per Django ArrayField documentation, you should not use [] as the default. It won’t cause this problem, but it will probably create some others! You should use default=list instead of default=[], which will create a mutable default shared between all instances of ArrayField:

Instead of:

mark7=ArrayField(models.CharField(max_length=5),default=[])

Try:

mark7=ArrayField(models.CharField(max_length=5),default=list)
πŸ‘€Joe Davy

1πŸ‘

I stupidly used this specific posgresql field – ArrayField for the model and let the test run with sqlite. That caused the error when I pushed code to github with the travis-ci.

πŸ‘€Dat TT

1πŸ‘

The fix is to simply remove the new migration files which has not been applied. You can see those by running the command.

python manage.py showmigrations

The files against which the β€˜tick’ is absent are the ones that can be safely removed. Once removed, then go to the models.py and remove the ArrayField field.

Run the makemigrations command again followed by the migrate command. This time it should work.

If you want to use the ArrayField field then you should use postgres database in your project.

0πŸ‘

The ArrayField docs warn that you shouldn’t use a mutable value like []. You can use the callable list for an empty default.

class Test(models.Model):
    ...
    mark3=ArrayField(models.CharField(max_length=5), default=list)
    mark4=ArrayField(models.CharField(max_length=5), default=list)
    mark7=ArrayField(models.CharField(max_length=5), default=list)

After making this change, delete the old migration file and run makemigrations again.

πŸ‘€Alasdair

Leave a comment