django.db.utils.OperationalError:near“[]":语法错误

eaf3rand  于 2023-07-01  发布在  Go
关注(0)|答案(4)|浏览(159)

有这个错误显示后,我已经使用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
8dtrkrch

8dtrkrch1#

我也为这个问题困惑了一会儿。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)
alen0pnh

alen0pnh2#

我愚蠢地使用了这个特定的posgresql字段- ArrayField作为模型,并让测试运行sqlite。这导致了我用travis-ci将代码推送到github时的错误。

x759pob2

x759pob23#

ArrayField文档警告您不应使用像[]这样的可变值。您可以使用可调用的list作为空默认值。

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)

完成此更改后,删除旧的迁移文件并再次运行makemigrations

llmtgqce

llmtgqce4#

修复方法是简单地删除尚未应用的新迁移文件。您可以通过运行命令来查看这些。

python manage.py showmigrations

不存在“tick”的文件是可以安全删除的文件。删除后,转到models.py并删除ArrayField字段。
再次运行makemigrations命令,然后运行migrate命令。这一次它应该工作。
如果你想使用ArrayField字段,那么你应该在项目中使用postgres数据库。

相关问题