rushabh shah
rushabh shah

Reputation: 192

django.db.utils.OperationalError: near "[]": syntax error

There is this error showing after I have used makemigrations command I have tried commenting different column for it but it wont work

    C:\Users\Rushabh\Desktop\project\MyPrj>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, paper, sessions
Running migrations:
  Applying paper.0014_auto_20170405_1549...Traceback (most recent call last):
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "[]": syntax error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\sqlite3\schema.py", line 191, in _remake_table
    self.create_model(temp_model)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\base\schema.py", line 295, in create_model
    self.execute(sql, params or None)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
    cursor.execute(sql, params)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "[]": syntax error

Migration file

from __future__ import unicode_literals

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('paper', '0019_auto_20170405_1659'),
    ]

    operations = [
        migrations.AddField(
            model_name='test',
            name='checked',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='test',
            name='mark3',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=5), default=[], size=None),
        ),
        migrations.AddField(
            model_name='test',
            name='mark4',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=5), default=[], size=None),
        ),
        migrations.AddField(
            model_name='test',
            name='mark7',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=5), default=[], size=None),
        ),
        migrations.AddField(
            model_name='test',
            name='request',
            field=models.BooleanField(default=False),
        ),
    ]

The problem arose after I added the following field to my models

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

The models.py file is

from django.db import models
from django.contrib.postgres.fields import ArrayField

class User(models.Model):
    user_id=models.CharField(unique=True,max_length=50)
    password=models.CharField(max_length=50)
    role=models.IntegerField(blank=False)
    def __str__(self):
        return self.user_id


class Qbank(models.Model):
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    qbank_id=models.CharField(unique=True,max_length=50,blank=False)
    # subject_id=models.CharField(max_length=50)
    qbank_file=models.FileField(upload_to= 'qbs/',blank=False)
    ans_file=models.FileField(upload_to= 'ans/',blank=False)
    uploaded_at = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.qbank_id


class Test(models.Model):
    qbank=models.ForeignKey(Qbank,on_delete=models.CASCADE)
    test_id=models.CharField(unique=True,max_length=50)
    request=models.BooleanField(default=False)
    checked=models.BooleanField(default=False)
    mark3=ArrayField(models.CharField(max_length=5),default=[])
    mark4=ArrayField(models.CharField(max_length=5),default=[])
    mark7=ArrayField(models.CharField(max_length=5),default=[])
    # true if send and checked respt
    def __str__(self):
        return self.test_id

Upvotes: 18

Views: 18956

Answers (3)

Chandragupta Borkotoky
Chandragupta Borkotoky

Reputation: 1756

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.

Upvotes: 1

Dat TT
Dat TT

Reputation: 3083

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.

Upvotes: 1

Joe Davy
Joe Davy

Reputation: 783

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)

Upvotes: 30

Related Questions