我不想使用默认的主键 ,因此我自定义了一个名为cid的主键,但是我什至无法保存一个数据。 有人帮我吗?
我的代码(peewee 3.9.2):
from peewee import *
db = SqliteDatabase(r'peewee.db')
class BaseModel(Model):
class Meta:
database=db
class Case(BaseModel):
cid=IntegerField(primary_key=True)
db.create_tables([Case,])
c1=Case(cid=1)
c1.save()
回溯(最近通话最近一次):
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 2835, in execute_sql
cursor.execute(sql, params or ())
sqlite3.OperationalError: near "WHERE": syntax error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.2\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2018.2\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2018.2\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/sanakan/PycharmProjects/xingshianjian/orm.py", line 12, in <module>
c1.save()
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 5914, in save
rows = self.update(**field_dict).where(self._pk_expr()).execute()
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 1698, in inner
return method(self, database, *args, **kwargs)
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 1769, in execute
return self._execute(database)
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 2236, in _execute
cursor = database.execute(self)
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 2848, in execute
return self.execute_sql(sql, params, commit=commit)
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 2842, in execute_sql
self.commit()
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 2627, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 178, in reraise
raise value.with_traceback(tb)
File "C:\Users\pcell\AppData\Local\Programs\Python\Python36\lib\site-packages\peewee.py", line 2835, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: near "WHERE": syntax error
答案 0 :(得分:0)
IntegerField(primary_key=True)
与AutoField()
不同。如果要自动递增整数ID,则应使用AutoField
:
cid = AutoField() # Implies primary_key=True
如果要手动指定整数ID,则可以使用:
cid = IntegerField(primary_key=True) # You will specify the ID values.
使用自动字段时,一切都应按预期工作。
在使用非自动递增的主键时,如IntegerField(primary_key=True)
,Peewee无法知道何时调用.save()
是创建(INSERT)还是更新。因此,在使用用户指定的主键时,需要调用.save(force_insert=True)
来生成INSERT。否则peewee会认为您的意思是更新。
http://docs.peewee-orm.com/en/latest/peewee/models.html#primary-keys-composite-keys-and-other-tricks