question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

If custom primary_key is used, .save() with no PK doesn't work properly

See original GitHub issue

The 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:closed
  • Created 8 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
foxxcommented, Jul 10, 2015

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.

0reactions
coleifercommented, Jan 20, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found