Opened 17 years ago
Closed 14 years ago
#5309 closed (fixed)
[Patch] Marking instances as db-stored via is_stored
Reported by: | 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)
Change History (16)
by , 17 years ago
Attachment: | base.py.diff added |
---|
by , 17 years ago
Attachment: | query.py.diff added |
---|
by , 17 years ago
Attachment: | base.py.2.diff added |
---|
by , 17 years ago
Attachment: | query.py.2.diff added |
---|
comment:1 by , 17 years ago
comment:2 by , 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 , 17 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:4 by , 17 years ago
Triage Stage: | Design decision needed → Accepted |
---|
I like this a lot -- it's a smart optimization.
comment:5 by , 17 years ago
Triage Stage: | Accepted → Design 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 , 17 years ago
Keywords: | feature added |
---|
comment:7 by , 17 years ago
Design decision would be really good now. This isn't a feature, its a bug fix :)
comment:8 by , 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.
follow-up: 10 comment:9 by , 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).
comment:10 by , 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 , 15 years ago
Cc: | removed |
---|
comment:12 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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.
Fixed diff's, and changed is_stored to _is_stored.