添加数组字段 OperationalError
Adding array field 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 但是迁移期间的错误没有 change.And 现在我不能迁移任何类型的任何字段!
这是我的错误:
E:\django projects\third>python manage.py makemigrations
Migrations for 'shop':
shop\migrations[=11=]13_auto_20180611_1518.py
- Alter field basket on order
E:\django projects\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\third>
我还将数组字段的名称从 prds 更改为 basket 但您可以在错误中看到名称没有更改!!!!!!它运行 0005_order_prds 尽管最后创建的迁移是 00013_etc !
我认为你的问题是你使用的是 SQLite 数据库,你需要使用 Postgres 数据库 -
我是 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 但是迁移期间的错误没有 change.And 现在我不能迁移任何类型的任何字段!
这是我的错误:
E:\django projects\third>python manage.py makemigrations
Migrations for 'shop':
shop\migrations[=11=]13_auto_20180611_1518.py
- Alter field basket on order
E:\django projects\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\third>
我还将数组字段的名称从 prds 更改为 basket 但您可以在错误中看到名称没有更改!!!!!!它运行 0005_order_prds 尽管最后创建的迁移是 00013_etc !
我认为你的问题是你使用的是 SQLite 数据库,你需要使用 Postgres 数据库 -