2017-04-05 139 views
3

没有显示我已经使用后makemigrations这个错误命令 我曾尝试为它注释不同列,但它不会工作django.db.utils.OperationalError:近“[]”:语法错误

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 

回答

0

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

+0

现在的错误是没有这样的列:paper_test.mark3 –

+0

对不起,这听起来像是一个单独的问题,我不能告诉你所提供的信息的问题。如果这是一个使用新模型的测试项目,最简单的修复方法可能是删除数据库,删除迁移文件,创建新的迁移并迁移。 – Alasdair

3

我也对这个问题感到困惑。该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)