From b0bc8b9dfd6069e53cd73c1d3411f995a05f0a5a Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 30 Jun 2008 00:38:14 +0000 Subject: [PATCH] Fixed #7565 -- Fixed a problem with PostgreSQL sequence resetting in loaddata. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7789 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/postgresql/operations.py | 2 +- .../fixtures/model-inheritance.json | 4 ++++ .../fixtures_regress/models.py | 21 +++++++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 tests/regressiontests/fixtures_regress/fixtures/model-inheritance.json diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index 7e27b23f723..ba6e3235c22 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -97,7 +97,7 @@ class DatabaseOperations(BaseDatabaseOperations): # Use `coalesce` to set the sequence for each model to the max pk value if there are records, # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true # if there are records (as the max pk value is already in use), otherwise set it to false. - for f in model._meta.fields: + for f in model._meta.local_fields: if isinstance(f, models.AutoField): output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ (style.SQL_KEYWORD('SELECT'), diff --git a/tests/regressiontests/fixtures_regress/fixtures/model-inheritance.json b/tests/regressiontests/fixtures_regress/fixtures/model-inheritance.json new file mode 100644 index 00000000000..00c482b3dda --- /dev/null +++ b/tests/regressiontests/fixtures_regress/fixtures/model-inheritance.json @@ -0,0 +1,4 @@ +[ + {"pk": 1, "model": "fixtures_regress.parent", "fields": {"name": "fred"}}, + {"pk": 1, "model": "fixtures_regress.child", "fields": {"data": "apple"}} +] diff --git a/tests/regressiontests/fixtures_regress/models.py b/tests/regressiontests/fixtures_regress/models.py index 59fc167d50b..56ed73169d2 100644 --- a/tests/regressiontests/fixtures_regress/models.py +++ b/tests/regressiontests/fixtures_regress/models.py @@ -20,7 +20,7 @@ class Plant(models.Model): class Stuff(models.Model): name = models.CharField(max_length=20, null=True) owner = models.ForeignKey(User, null=True) - + def __unicode__(self): # Oracle doesn't distinguish between None and the empty string. # This hack makes the test case pass using Oracle. @@ -38,13 +38,19 @@ class Absolute(models.Model): super(Absolute, self).__init__(*args, **kwargs) Absolute.load_count += 1 +class Parent(models.Model): + name = models.CharField(max_length=10) + +class Child(Parent): + data = models.CharField(max_length=10) + __test__ = {'API_TESTS':""" >>> from django.core import management # Load a fixture that uses PK=1 >>> management.call_command('loaddata', 'sequence', verbosity=0) - + # Create a new animal. Without a sequence reset, this new object # will take a PK of 1 (on Postgres), and the save will fail. # This is a regression test for ticket #3790. @@ -61,9 +67,9 @@ __test__ = {'API_TESTS':""" [] ############################################### -# Regression test for ticket #6436 -- +# Regression test for ticket #6436 -- # os.path.join will throw away the initial parts of a path if it encounters -# an absolute path. This means that if a fixture is specified as an absolute path, +# an absolute path. This means that if a fixture is specified as an absolute path, # we need to make sure we don't discover the absolute path in every fixture directory. >>> load_absolute_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'absolute.json') @@ -94,4 +100,11 @@ No fixture data found for 'bad_fixture2'. (File format may be invalid.) >>> sys.stderr = savestderr +############################################### +# Test for ticket #7565 -- PostgreSQL sequence resetting checks shouldn't +# ascend to parent models when inheritance is used (since they are treated +# individually). + +>>> management.call_command('loaddata', 'model-inheritance.json', verbosity=0) + """}