In some situations, the Python module Peewee will reset the (non-integer) primary key when saving an object. I have constructed this example to clarify:
#!/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
An example output:
$ ./pkey.py
0bd49fa9-c5cc-40e7-aff7-24e0b17247cb
0bd49fa9-c5cc-40e7-aff7-24e0b17247cb
--
2fe23bac-4cb2-46a2-827a-8a1c6395e665
1
Now, the last line should not be 1, but rather 2fe... as the line above. The funny thing is, this, as the example shows, only happens to the child object.
Am I completely misunderstanding something here?
I have addressed this issue on GitHub and posted a fix. The problem was caused by a bug in the inheritance of primary key fields from model to model.