使用 makemigrations 命令后显示此错误
我曾尝试为它评论不同的专栏,但它不起作用

    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

迁移文件
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),
        ),
    ]

在我将以下字段添加到我的模型后出现了问题
mark3=ArrayField(models.CharField(max_length=5),default=[])

models.py 文件是
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

最佳答案

我也对这个问题困惑了一会儿。 ArrayField 特定于 Postgres,并且是从 Postgres 库导入的:

import django.contrib.postgres.fields

看起来您正在尝试将迁移提交到 SQLite。您应该设置一个本地 Postgres 数据库,并从以下位置更新您的 settings.py 文件:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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

此外,您错误地设置了ArrayField的默认值。根据 Django ArrayField documentation ,您不应使用 [] 作为默认值。它不会导致这个问题,但它可能会产生其他一些问题!您应该使用 default=list 而不是 default=[] ,这将创建一个在 ArrayField 的所有实例之间共享的可变默认值:

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

尝试:
mark7=ArrayField(models.CharField(max_length=5),default=list)

关于django.db.utils.OperationalError : near "[]": syntax error,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43231618/

10-12 22:45