使用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
答案 0 :(得分:4)
我也困惑了一下这个。 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的默认值。根据{{3}},您不应使用[]作为默认值。它不会导致这个问题,但它可能会创造一些其他的!您应该使用default=list
而不是default=[]
,这将在ArrayField的所有实例之间创建一个可变的默认共享:
而不是:
mark7=ArrayField(models.CharField(max_length=5),default=[])
尝试:
mark7=ArrayField(models.CharField(max_length=5),default=list)
答案 1 :(得分:1)
我愚蠢地使用了这个特定的posgresql字段-ArrayField作为模型,并使用sqlite运行测试。当我使用travis-ci将代码推送到github时,这导致了错误。
答案 2 :(得分: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
。