Opened 17 years ago

Closed 14 years ago

#5309 closed (fixed)

[Patch] Marking instances as db-stored via is_stored

Reported by: David Cramer <dcramer@…> Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: is_stored feature
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

This patch adds a flag to instance called 'is_stored'. By default it is set to None. When a DB instance is available in the database it is True, and when its removed it is False.

This corrects the need for Django guessing based on the PK value if it needs to try to do an update or insert, and should throw exceptions if you try to do an insert (.create) when a matching pk exists.

Attachments (4)

base.py.diff (2.1 KB ) - added by David Cramer <dcramer@…> 17 years ago.
query.py.diff (1.3 KB ) - added by David Cramer <dcramer@…> 17 years ago.
base.py.2.diff (2.1 KB ) - added by David Cramer <dcramer@…> 17 years ago.
query.py.2.diff (1.3 KB ) - added by David Cramer <dcramer@…> 17 years ago.

Download all attachments as: .zip

Change History (16)

by David Cramer <dcramer@…>, 17 years ago

Attachment: base.py.diff added

by David Cramer <dcramer@…>, 17 years ago

Attachment: query.py.diff added

by David Cramer <dcramer@…>, 17 years ago

Attachment: base.py.2.diff added

by David Cramer <dcramer@…>, 17 years ago

Attachment: query.py.2.diff added

comment:1 by David Cramer <dcramer@…>, 17 years ago

Fixed diff's, and changed is_stored to _is_stored.

comment:2 by anonymous, 17 years ago

Needs documentation: set
Needs tests: set
Patch needs improvement: set

For future reference - use one patch, not two, and you won't win any friends without tests :-)

comment:3 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedDesign decision needed

comment:4 by Adrian Holovaty, 17 years ago

Triage Stage: Design decision neededAccepted

I like this a lot -- it's a smart optimization.

comment:5 by Adrian Holovaty, 17 years ago

Triage Stage: AcceptedDesign decision needed

Hmm, I just realized there's a wide-open race condition here -- if an instance is marked as is_stored, then is deleted by another process, the save() method will be fooled into doing an UPDATE, and no records will be updated.

comment:6 by Philippe Raoult, 17 years ago

Keywords: feature added

comment:7 by David Cramer, 16 years ago

Design decision would be really good now. This isn't a feature, its a bug fix :)

comment:8 by Johannes Dollinger, 16 years ago

-1

You would also have to track primary key changes to clear _is_stored, which doesn't seem worth it given that you never know whether the object is still in the db (see adrian's comment).

Explicit update/insert support would be more useful to optimize/prevent bugs where it is required.

comment:9 by David Cramer, 16 years ago

Even if the primary key is changed it doesn't change the fact that it's stored. Are you saying that when you change the primary key of a row it should create a new row? (Thinking about it, I'd bet Django doesn't handle this, but it should).

in reply to:  9 comment:10 by Johannes Dollinger, 16 years ago

Replying to dcramer:

Even if the primary key is changed it doesn't change the fact that it's stored. Are you saying that when you change the primary key of a row it should create a new row? (Thinking about it, I'd bet Django doesn't handle this, but it should).

No, django doesn't handle changing pks via .save() - and it would be quite painful: you had to update all related objects as well. IMO, changing primary keys is mostly unnecessary. So yes, I'd say creating a new row (or updating an existent row) is the right thing to do when you change the pk.

comment:11 by (removed), 14 years ago

Cc: ferringb@… removed

comment:12 by Malcolm Tredinnick, 14 years ago

Resolution: fixed
Status: newclosed

This seems to be solved via the addition of force_update and force_insert. That saves the guessing because you already know how you got the object.

Note: See TracTickets for help on using tickets.
Back to Top