在某些情況下,Python模塊Peewee會在保存對象時重置(非整數)主鍵。我構建這個例子來闡明:Python Peewee重置主鍵
#!/usr/bin/python
from peewee import *
import uuid
db = SqliteDatabase('./db/' + str(uuid.uuid4()) + '.db')
class A(Model):
id = CharField(primary_key=True)
def __init__(self):
super(A, self).__init__()
self.id = str(uuid.uuid4())
class Meta:
database = db
class B(A):
name = CharField()
def __init__(self, name):
super(B, self).__init__()
self.name = name
A.create_table()
a = A()
print a.id
a.save(force_insert=True)
print a.id
print "--"
B.create_table()
b = B(name='Test')
print b.id
b.save(force_insert=True)
print b.id
輸出示例:
$ ./pkey.py
0bd49fa9-c5cc-40e7-aff7-24e0b17247cb
0bd49fa9-c5cc-40e7-aff7-24e0b17247cb
--
2fe23bac-4cb2-46a2-827a-8a1c6395e665
1
現在,最後一行不應該是1,而是2FE ......作爲線之上。有趣的是,正如例子所示,這隻發生在子對象上。
我完全誤解了一些東西嗎?
我張貼在這個bug報告[https://github.com/coleifer/peewee/issues/175](https://github.com/coleifer/peewee/issues/175) - 開發人員認識到這個問題,並且會研究它。 – henrikstroem 2013-03-24 16:59:58
現在已經修復 - 應該在下一個版本中工作。 [https://github.com/coleifer/peewee/commit/69a295046ad9f1cfc38d2a1d4b688cfd25b2c65f](https://github.com/coleifer/peewee/commit/69a295046ad9f1cfc38d2a1d4b688cfd25b2c65f) – henrikstroem 2013-03-24 20:32:28