新创建的save()上的peewee错误

时间:2017-10-10 11:53:53

标签: python peewee

让自己熟悉peewee,我偶然发现了以下奇怪的行为:给出类似于此的类

class Test(BaseModel):
    str = CharField(primary_key=True)
执行

instance = Test(str = 'something')
instance.save()

提出了一个模糊的错误,抱怨某些'WHERE':

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3830, 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 "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 5165, in save
    rows = self.update(**field_dict).where(self._pk_expr()).execute()
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3458, in execute
    return self.database.rows_affected(self._execute())
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2939, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3837, in execute_sql
    self.commit()
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3656, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3830, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: near "WHERE": syntax error

但是,使用

直接创建实例
instance = Test.create(str = 'something')

按预期工作。只有在使用的属性是主键时才会发生这种情况。 类似的问题似乎是this问题,其中没有产生错误,但也没有数据库条目。我知道解决方案的解决方法,但只是对这种奇怪的行为感到好奇。

1 个答案:

答案 0 :(得分:0)

使用非自动递增主键时,需要使用save(force_insert = True)。

http://docs.peewee-orm.com/en/latest/peewee/models.html#non-integer-primary-keys-composite-keys-and-other-tricks