Django

Code

Ticket #757 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

Cannot set primary key value manually for new objects.

Reported by: cygnus@cprogrammer.org Assigned to: adrian
Milestone: Component: Core framework
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

When calling object.save() for objects whose primary key values *do not* exist in the model table, an auto-assigned primary key value is used instead of the one that I assign in my program. For example, this code will result in a primary key based on a db.get_last_insert_id call rather than the value I assign (assuming no record with id = 15 exists in the model table):

obj = SomeModel()
obj.id = 15
obj.save()

The object.save() code in meta/__init__.py assumes that if the ID does not already exist, an INSERT must take place and any auto-assign fields must get their values accordingly.

Attachments

pk_save.diff (1.6 kB) - added by cygnus@cprogrammer.org on 11/08/05 17:49:22.
Patch to make saving primary key values work.

Change History

11/08/05 17:49:22 changed by cygnus@cprogrammer.org

  • attachment pk_save.diff added.

Patch to make saving primary key values work.

11/08/05 17:51:28 changed by cygnus@cprogrammer.org

I've added a small patch to fix this issue, but I cannot attest to whether other issues remain (for example, whether calling f.pre_save() on some AutoField f will never be problematic). I've tested this and it works as expected under the circumstances I described.

11/30/05 21:06:31 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

(In [1511]) Fixed #757: manually set AutoField? values are now respected; also added unit test to verify the correct behavior. Thanks, cygnus.


Add/Change #757 (Cannot set primary key value manually for new objects.)




Change Properties
Action