If custom primary_key is used, .save() with no PK doesn't work properly
See original GitHub issueThe following code does not raise any exceptions, but it doesn’t save to the database either.
import peewee
import random
db = peewee.SqliteDatabase(':memory:')
class Message(peewee.Model):
class Meta:
database = db
content = peewee.TextField()
class MessageCustomPrimaryKey(peewee.Model):
class Meta:
database = db
id = peewee.TextField(default=1, primary_key=True, unique=True)
content = peewee.TextField()
db.create_table(Message)
db.create_table(MessageCustomPrimaryKey)
# this works as expected
m = Message()
m.content = 'hello'
m.save()
assert m.id == 1
assert Message.select().count() == 1
# this does not
m = MessageCustomPrimaryKey()
m.content = 'hello'
m.save()
assert m.id == 1
assert MessageCustomPrimaryKey.select().count() == 1
Although creating objects in this pattern isn’t very clean, it was a surprising result, because if the record was not saved to the backend then I would expect some sort of exception to be raised.
If I remove the custom id
primary_key field, the problem goes away. I’ve trawled through the code and documentation, but cannot find anything to explain why this is happening.
Thoughts?
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
In a django model custom save() method, how should you ...
Checking self.id assumes that id is the primary key for the model. ... column for the primary key, this doesn't hold for other...
Read more >Add or change a table's primary key in Access
A primary key is a field or set of fields with values that are unique throughout a table. Values of the key can...
Read more >Models | Django documentation
Each attribute of the model represents a database field. ... If you'd like to specify a custom primary key, specify primary_key=True on one...
Read more >Cannot correctly set primary key · Issue #8
I use simple .save() and .delete() functions in my views. But I face something that seems to be a collision between django and...
Read more >Model instance reference — Django 4.1.4 documentation
If a model has an AutoField — an auto-incrementing primary key — then that auto-incremented value will be calculated and saved as an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fwiw, do you think it’s worth patching so that an exception is raised if this situation is ever encountered? Although it’s documented, it would be nice to have an exception raised in the event of human error maybe.
It does work, Peewee just assumes if the PK is present that it should do an update instead. Peewee is an active-record ORM. This is fundamental stuff.