From 93f60163e87036dac3dbfbbe09bdd7c7c195dd67 Mon Sep 17 00:00:00 2001 From: Ian Kelly Date: Fri, 14 Sep 2007 18:12:36 +0000 Subject: [PATCH] Fixed #5218: Made Oracle create autoinc triggers using the correct name of the AutoField column rather than always assume "ID". git-svn-id: http://code.djangoproject.com/svn/django/trunk@6195 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/sql.py | 5 +++-- django/db/backends/__init__.py | 2 +- django/db/backends/oracle/base.py | 15 +++++++++------ django/db/models/fields/__init__.py | 1 + django/db/models/options.py | 2 +- tests/regressiontests/model_regress/models.py | 5 +++++ 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/django/core/management/sql.py b/django/core/management/sql.py index 8f7f6a023a..ed76bc4e35 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -302,7 +302,8 @@ def sql_model_create(model, style, known_models=set()): if opts.has_auto_field: # Add any extra SQL needed to support auto-incrementing primary keys. - autoinc_sql = connection.ops.autoinc_sql(opts.db_table) + auto_column = opts.auto_field.db_column or opts.auto_field.name + autoinc_sql = connection.ops.autoinc_sql(opts.db_table, auto_column) if autoinc_sql: for stmt in autoinc_sql: final_output.append(stmt) @@ -385,7 +386,7 @@ def many_to_many_sql_for_model(model, style): final_output.append('\n'.join(table_output)) # Add any extra SQL needed to support auto-incrementing PKs - autoinc_sql = connection.ops.autoinc_sql(f.m2m_db_table()) + autoinc_sql = connection.ops.autoinc_sql(f.m2m_db_table(), 'id') if autoinc_sql: for stmt in autoinc_sql: final_output.append(stmt) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index f3c6f59258..1b6ba07f24 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -56,7 +56,7 @@ class BaseDatabaseOperations(object): a backend performs ordering or calculates the ID of a recently-inserted row. """ - def autoinc_sql(self, table): + def autoinc_sql(self, table, column): """ Returns any SQL needed to support auto-incrementing primary keys, or None if no SQL is necessary. diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 23ce30f37e..4093b69be6 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -31,20 +31,23 @@ class DatabaseFeatures(BaseDatabaseFeatures): uses_custom_queryset = True class DatabaseOperations(BaseDatabaseOperations): - def autoinc_sql(self, table): + def autoinc_sql(self, table, column): # To simulate auto-incrementing primary keys in Oracle, we have to # create a sequence and a trigger. sq_name = get_sequence_name(table) tr_name = get_trigger_name(table) + tbl_name = self.quote_name(table) + col_name = self.quote_name(column) sequence_sql = 'CREATE SEQUENCE %s;' % sq_name trigger_sql = """ - CREATE OR REPLACE TRIGGER %s - BEFORE INSERT ON %s + CREATE OR REPLACE TRIGGER %(tr_name)s + BEFORE INSERT ON %(tbl_name)s FOR EACH ROW - WHEN (new.id IS NULL) + WHEN (new.%(col_name)s IS NULL) BEGIN - SELECT %s.nextval INTO :new.id FROM dual; - END;/""" % (tr_name, self.quote_name(table), sq_name) + SELECT %(sq_name)s.nextval + INTO :new.%(col_name)s FROM dual; + END;/""" % locals() return sequence_sql, trigger_sql def date_extract_sql(self, lookup_type, field_name): diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 3792a30a88..34d998c219 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -433,6 +433,7 @@ class AutoField(Field): assert not cls._meta.has_auto_field, "A model can't have more than one AutoField." super(AutoField, self).contribute_to_class(cls, name) cls._meta.has_auto_field = True + cls._meta.auto_field = self def formfield(self, **kwargs): return None diff --git a/django/db/models/options.py b/django/db/models/options.py index ad813ae0f7..502cbc4a65 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -33,7 +33,7 @@ class Options(object): self.admin = None self.meta = meta self.pk = None - self.has_auto_field = False + self.has_auto_field, self.auto_field = False, None self.one_to_one_field = None self.parents = [] diff --git a/tests/regressiontests/model_regress/models.py b/tests/regressiontests/model_regress/models.py index 7aa9e2a7c4..00c3bc96f0 100644 --- a/tests/regressiontests/model_regress/models.py +++ b/tests/regressiontests/model_regress/models.py @@ -20,6 +20,11 @@ class Article(models.Model): def __unicode__(self): return self.headline +class Movie(models.Model): + #5218: Test models with non-default primary keys / AutoFields + movie_id = models.AutoField(primary_key=True) + name = models.CharField(max_length=60) + __test__ = {'API_TESTS': """ (NOTE: Part of the regression test here is merely parsing the model declaration. The verbose_name, in particular, did not always work.)