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:UsersRushabhDesktopprojectMyPrj>python manage.py

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

    C:UsersRushabhDesktopprojectMyPrj>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:UsersRushabhAnaconda3libsite-packagesdjangodbbackendsutils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendssqlite3base.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:UsersRushabhAnaconda3libsite-packagesdjangocoremanagement__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:UsersRushabhAnaconda3libsite-packagesdjangocoremanagement__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangocoremanagementbase.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangocoremanagementbase.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangocoremanagementcommandsmigrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbmigrationsexecutor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbmigrationsexecutor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbmigrationsexecutor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbmigrationsmigration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbmigrationsoperationsfields.py", line 84, in database_forwards
    field,
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendssqlite3schema.py", line 231, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendssqlite3schema.py", line 191, in _remake_table
    self.create_model(temp_model)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendsbaseschema.py", line 295, in create_model
    self.execute(sql, params or None)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendsbaseschema.py", line 112, in execute
    cursor.execute(sql, params)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendsutils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbutils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangoutilssix.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendsutils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:UsersRushabhAnaconda3libsite-packagesdjangodbbackendssqlite3base.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

I don’t have a clue what’s causing this error. It appears to be a bug that there isn’t a fix for. Could anyone tell give me a hint as to how I might get around this? It’s frustrating me to no end. Thanks.

Operations to perform:
  Apply all migrations: admin, contenttypes, optilab, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying optilab.0006_auto_20160621_1640...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:Python27libsite-packagesdjangocoremanagementbase.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:Python27libsite-packagesdjangocoremanagementbase.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:Python27libsite-packagesdjangocoremanagementcommandsmigrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:Python27libsite-packagesdjangodbmigrationsexecutor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:Python27libsite-packagesdjangodbmigrationsexecutor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:Python27libsite-packagesdjangodbmigrationsexecutor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:Python27libsite-packagesdjangodbmigrationsmigration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:Python27libsite-packagesdjangodbmigrationsoperationsfields.py", line 121, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "C:Python27libsite-packagesdjangodbbackendssqlite3schema.py", line 247, in remove_field
    self._remake_table(model, delete_fields=[field])
  File "C:Python27libsite-packagesdjangodbbackendssqlite3schema.py", line 197, in _remake_table
    self.quote_name(model._meta.db_table),
  File "C:Python27libsite-packagesdjangodbbackendsbaseschema.py", line 110, in execute
    cursor.execute(sql, params)
  File "C:Python27libsite-packagesdjangodbbackendsutils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:Python27libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:Python27libsite-packagesdjangodbutils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:Python27libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:Python27libsite-packagesdjangodbbackendssqlite3base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error

Here’s the contents of 0006_auto_20160621_1640.py

# -*- coding: utf-8 -*-

# Generated by Django 1.9.6 on 2016-06-21 22:40
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('optilab', '0005_test'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='lasersubstrate',
            name='substrate_ptr',
        ),
        migrations.RemoveField(
            model_name='waveguidesubstrate',
            name='substrate_ptr',
        ),
        migrations.DeleteModel(
            name='LaserSubstrate',
        ),
        migrations.DeleteModel(
            name='WaveguideSubstrate',
        ),
    ]

Here’s the SQL produced from running ‘python manage.py sqlmigrate optilab 0006’

BEGIN;
--
-- Remove field substrate_ptr from lasersubstrate
--
ALTER TABLE "optilab_lasersubstrate" RENAME TO "optilab_lasersubstrate__old";
CREATE TABLE "optilab_lasersubstrate" ("substrate_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "optilab_substrate" ("id"));
INSERT INTO "optilab_lasersubstrate" () SELECT  FROM "optilab_lasersubstrate__old";
DROP TABLE "optilab_lasersubstrate__old";
--
-- Remove field substrate_ptr from waveguidesubstrate
--
ALTER TABLE "optilab_waveguidesubstrate" RENAME TO "optilab_waveguidesubstrate__old";
CREATE TABLE "optilab_waveguidesubstrate" ("substrate_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "optilab_substrate" ("id"));
INSERT INTO "optilab_waveguidesubstrate" () SELECT  FROM "optilab_waveguidesubstrate__old";
DROP TABLE "optilab_waveguidesubstrate__old";
--
-- Delete model LaserSubstrate
--
DROP TABLE "optilab_lasersubstrate";
--
-- Delete model WaveguideSubstrate
--
DROP TABLE "optilab_waveguidesubstrate";

COMMIT;

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)

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.

#28274

closed


Bug


(needsinfo)

Reported by: Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: SQLite pytest
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

I am running some test, but I keep getting this error:

self = <django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0x7f619aa32d38>
query = '        SELECT cust.id, cust.name, inv.currency_id, SUM(inv.total)n        FROMn          v3_customer as custn    ...        and inv.invoice_date < ?n        GROUP BY cust.id, inv.currency_idn        ORDER BY cust.id, inv.currency_id'
params = [(1, 2, 3, 4), '2016-12-08']

    def execute(self, query, params=None):
        if params is None:
            return Database.Cursor.execute(self, query)
        query = self.convert_query(query)
>       return Database.Cursor.execute(self, query, params)
E       django.db.utils.OperationalError: near "?": syntax error

../../../environments/tracerenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py:337: OperationalError

When looking at the SQL statements and trying to find the «?» symbol it does not appear:

PRE_INV_Q = """
        SELECT cust.id, cust.name, inv.currency_id, SUM(inv.total)
        FROM
          v3_customer as cust
          JOIN v3_customerproxy ON cust.id = v3_customerproxy.original_id
          JOIN v3_invoice as inv ON v3_customerproxy.id = inv.customer_id
        WHERE
          cust.id IN %s
          and inv.type = 'i'
          and inv.invoice_date < %s
        GROUP BY cust.id, inv.currency_id
        ORDER BY cust.id, inv.currency_id"""

Could there be some incompatibilities between Postgre and SQLite?

Change History (11)

Description: modified (diff)
Description: modified (diff)
Type: Uncategorized
Bug
Description: modified (diff)
Description: modified (diff)
Component: Uncategorized
Database layer (models, ORM)
Resolution: needsinfo
Status: new
closed

Note: See
TracTickets for help on using
tickets.

Back to Top

Я понятия не имею, что вызывает эту ошибку. Похоже, это ошибка, для которой нет исправления. Может ли кто-нибудь сказать мне, как я могу обойти это? Это бесконечно меня расстраивает. Спасибо.

Operations to perform:
  Apply all migrations: admin, contenttypes, optilab, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying optilab.0006_auto_20160621_1640...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:Python27libsite-packagesdjangocoremanagementbase.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:Python27libsite-packagesdjangocoremanagementbase.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:Python27libsite-packagesdjangocoremanagementcommandsmigrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:Python27libsite-packagesdjangodbmigrationsexecutor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:Python27libsite-packagesdjangodbmigrationsexecutor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:Python27libsite-packagesdjangodbmigrationsexecutor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:Python27libsite-packagesdjangodbmigrationsmigration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:Python27libsite-packagesdjangodbmigrationsoperationsfields.py", line 121, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "C:Python27libsite-packagesdjangodbbackendssqlite3schema.py", line 247, in remove_field
    self._remake_table(model, delete_fields=[field])
  File "C:Python27libsite-packagesdjangodbbackendssqlite3schema.py", line 197, in _remake_table
    self.quote_name(model._meta.db_table),
  File "C:Python27libsite-packagesdjangodbbackendsbaseschema.py", line 110, in execute
    cursor.execute(sql, params)
  File "C:Python27libsite-packagesdjangodbbackendsutils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:Python27libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:Python27libsite-packagesdjangodbutils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:Python27libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:Python27libsite-packagesdjangodbbackendssqlite3base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error

вот содержание 0006_auto_20160621_1640.py

# -*- coding: utf-8 -*-

# Generated by Django 1.9.6 on 2016-06-21 22:40
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('optilab', '0005_test'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='lasersubstrate',
            name='substrate_ptr',
        ),
        migrations.RemoveField(
            model_name='waveguidesubstrate',
            name='substrate_ptr',
        ),
        migrations.DeleteModel(
            name='LaserSubstrate',
        ),
        migrations.DeleteModel(
            name='WaveguideSubstrate',
        ),
    ]

вот SQL, созданный из запуска ‘python manage.py sqlmigrate optilab 0006’

BEGIN;
--
-- Remove field substrate_ptr from lasersubstrate
--
ALTER TABLE "optilab_lasersubstrate" RENAME TO "optilab_lasersubstrate__old";
CREATE TABLE "optilab_lasersubstrate" ("substrate_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "optilab_substrate" ("id"));
INSERT INTO "optilab_lasersubstrate" () SELECT  FROM "optilab_lasersubstrate__old";
DROP TABLE "optilab_lasersubstrate__old";
--
-- Remove field substrate_ptr from waveguidesubstrate
--
ALTER TABLE "optilab_waveguidesubstrate" RENAME TO "optilab_waveguidesubstrate__old";
CREATE TABLE "optilab_waveguidesubstrate" ("substrate_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "optilab_substrate" ("id"));
INSERT INTO "optilab_waveguidesubstrate" () SELECT  FROM "optilab_waveguidesubstrate__old";
DROP TABLE "optilab_waveguidesubstrate__old";
--
-- Delete model LaserSubstrate
--
DROP TABLE "optilab_lasersubstrate";
--
-- Delete model WaveguideSubstrate
--
DROP TABLE "optilab_waveguidesubstrate";

COMMIT;

это, кажется, строка, которая вызывает errror:

 INSERT INTO "optilab_lasersubstrate" () SELECT  FROM "optilab_lasersubstrate__old";

обычно ожидается, что у вас будет список столбцов в этих скобках. Например!—2—> однако миграция произвела пустой набор! Точно так же SELECT FROM часть следует читать как SELECT col1,col2 FROM. По какому-то странному набору событий Вам, похоже, удалось создать таблицу без столбцов!!

Я вижу из вашего файла миграции, что вы все равно отбрасываете эту таблицу. Так что нет никаких причин борьба с RemoveField часть. Это код, связанный с RemoveField это вызывает ошибку. Измените миграцию следующим образом:

class Migration(migrations.Migration):

    dependencies = [
        ('optilab', '0005_test'),
    ]

    operations = [
        migrations.DeleteModel(
            name='LaserSubstrate',
        ),
        migrations.DeleteModel(
            name='WaveguideSubstrate',
        ),
    ]

Понравилась статья? Поделить с друзьями:
  • Django custom error page
  • Django core exceptions improperlyconfigured error loading psycopg2 module no module named psycopg2
  • Django core exceptions improperlyconfigured error loading mysqldb module
  • Django authenticate error
  • Django assert error