From 4ed82677be94e98f0c3164db8bfdc7d520411526 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Tue, 26 Sep 2006 02:58:36 +0000 Subject: [PATCH] Fixed #2783 -- Fixed one-to-one fields to work with any primary key data type in the related model. Thanks, Joel Heenan. git-svn-id: http://code.djangoproject.com/svn/django/trunk@3846 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/core/management.py | 2 +- tests/modeltests/one_to_one/models.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 3c2542cb0d5..9621fd82529 100644 --- a/AUTHORS +++ b/AUTHORS @@ -82,6 +82,7 @@ answer newbie questions, and generally made Django that much better: Espen Grindhaug Brant Harris heckj@mac.com + Joel Heenan hipertracker@gmail.com Ian Holsman Kieran Holland diff --git a/django/core/management.py b/django/core/management.py index 37fe05cf42d..bcfcd60617e 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -147,7 +147,7 @@ def _get_sql_model_create(model, known_models=set()): table_output = [] pending_references = {} for f in opts.fields: - if isinstance(f, models.ForeignKey): + if isinstance(f, (models.ForeignKey, models.OneToOneField)): rel_field = f.rel.get_related_field() data_type = get_rel_data_type(rel_field) else: diff --git a/tests/modeltests/one_to_one/models.py b/tests/modeltests/one_to_one/models.py index 8afa74454d4..7488204ff1b 100644 --- a/tests/modeltests/one_to_one/models.py +++ b/tests/modeltests/one_to_one/models.py @@ -30,6 +30,14 @@ class Waiter(models.Model): def __str__(self): return "%s the waiter at %s" % (self.name, self.restaurant) +class ManualPrimaryKey(models.Model): + primary_key = models.CharField(maxlength=10, primary_key=True) + name = models.CharField(maxlength = 50) + +class RelatedModel(models.Model): + link = models.OneToOneField(ManualPrimaryKey) + name = models.CharField(maxlength = 50) + __test__ = {'API_TESTS':""" # Create a couple of Places. >>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton') @@ -151,4 +159,10 @@ DoesNotExist: Restaurant matching query does not exist. # Delete the restaurant; the waiter should also be removed >>> r = Restaurant.objects.get(pk=1) >>> r.delete() + +# One-to-one fields still work if you create your own primary key +>>> o1 = ManualPrimaryKey(primary_key="abc123", name="primary") +>>> o1.save() +>>> o2 = RelatedModel(link=o1, name="secondary") +>>> o2.save() """}