summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2005-12-01 03:06:30 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2005-12-01 03:06:30 +0000
commitc28daf228948a04d15f30e5861a659eb8171f73f (patch)
tree53f257500698b00d8206856eb83b5697f1a00d8f /django
parentbf0f6ec3b490fb0a9a44881f7151b3afb4ab4733 (diff)
Fixed #757: manually set AutoField values are now respected; also added unit test to verify the correct behavior. Thanks, cygnus.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1511 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/core/meta/__init__.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py
index 39ba4bc434..0af7fe51f1 100644
--- a/django/core/meta/__init__.py
+++ b/django/core/meta/__init__.py
@@ -1009,8 +1009,12 @@ def method_save(opts, self):
record_exists = False
if not pk_set or not record_exists:
field_names = [db.db.quote_name(f.column) for f in opts.fields if not isinstance(f, AutoField)]
- placeholders = ['%s'] * len(field_names)
db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), True)) for f in opts.fields if not isinstance(f, AutoField)]
+ # If the PK has been manually set we must respect that
+ if pk_set:
+ field_names += [f.column for f in opts.fields if isinstance(f, AutoField)]
+ db_values += [f.get_db_prep_save(f.pre_save(getattr(self, f.column), True)) for f in opts.fields if isinstance(f, AutoField)]
+ placeholders = ['%s'] * len(field_names)
if opts.order_with_respect_to:
field_names.append(db.db.quote_name('_order'))
# TODO: This assumes the database supports subqueries.
@@ -1020,7 +1024,7 @@ def method_save(opts, self):
cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
(db.db.quote_name(opts.db_table), ','.join(field_names),
','.join(placeholders)), db_values)
- if opts.has_auto_field:
+ if opts.has_auto_field and not pk_set:
setattr(self, opts.pk.attname, db.get_last_insert_id(cursor, opts.db_table, opts.pk.column))
db.db.commit()
# Run any post-save hooks.