添加数组字段OperationalError

时间:2018-06-11 22:30:17

标签: python django postgresql django-models operationalerror

我是django(版本1.11)的新手。我正在尝试建立一个购物网站,我很难创建订单模型。 在这里,您可以看到我的第一个(版本1)models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.contrib.postgres.fields import ArrayField


class Products(models.Model):
    name = models.CharField(max_length = 20 , default='')
    description = models.CharField(max_length = 100 , default='')
    price = models.IntegerField(default=0)
    image = models.ImageField(upload_to='product_image',blank=True)
    vojood = models.BooleanField(default=False)
    unique = models.CharField(max_length = 100 ,default='')
    typ = models.CharField(max_length = 100 ,default='')
    def __str__ (self):
        return self.name
    @classmethod
    def turn_on(prds,pk):
        prds[pk].vojood=true

    @classmethod
    def turn_off(prds,pk):
        prds[pk].vojood=false
class Order(models.Model):
    username = models.CharField(max_length = 300 , default='')
    address = models.CharField(max_length = 300 , default='')
    time = models.IntegerField(default=0)
    price = models.IntegerField(default=0)
    arrived = models.BooleanField(default=False)
    basket = ArrayField(models.CharField(max_length=300,default=''),default=list)
    def __str__ (self):
        return self.username

python manage.py makemigrations之后,当我尝试使用python manage.py migrate迁移新字段(arrayfield)时,我遇到了很长的错误。所以最后一行是django.db.utils.OperationalError: near "[]": syntax error所以我删除了新字段(arrayfield)并再次使用makemigrations但迁移过程中的错误没有改变。现在我无法迁移任何类型的任何字段! 这是我的错误:

E:\django projects\4\third>python manage.py makemigrations
Migrations for 'shop':
  shop\migrations\0013_auto_20180611_1518.py
    - Alter field basket on order

E:\django projects\4\third>python manage.py migrate
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, sessions, shop
Running migrations:
  Applying shop.0005_order_prds...Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 63, in
execute
    return self.cursor.execute(sql)
  File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line
326, 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:\Python34\lib\site-packages\django\core\management\__init__.py", line
364, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 283,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 330,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py
", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 11
5, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_i
nitial=fake_initial)
  File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 14
5, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 24
4, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python34\lib\site-packages\django\db\migrations\migration.py", line 1
29, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py"
, line 87, in database_forwards
    field,
  File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin
e 238, in add_field
    self._remake_table(model, create_field=field)
  File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin
e 198, in _remake_table
    self.create_model(temp_model)
  File "C:\Python34\lib\site-packages\django\db\backends\base\schema.py", line 3
03, in create_model
    self.execute(sql, params or None)
  File "C:\Python34\lib\site-packages\django\db\backends\base\schema.py", line 1
20, in execute
    cursor.execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 80, in
execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in
execute
    return self.cursor.execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python34\lib\site-packages\django\utils\six.py", line 685, in reraise

    raise value.with_traceback(tb)
  File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 63, in
execute
    return self.cursor.execute(sql)
  File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line
326, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "[]": syntax error

E:\django projects\4\third>

我还将数组字段的名称从prds更改为basket但你可以在错误中看到名称没有改变!!!!!尽管最后创建的迁移是00013_etc!

,它仍会运行0005_order_prds

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您使用的是SQLite数据库,需要使用Postgres数据库 - see this similar question.