Hi Lars,

I found myself in a similar position, and I'm also coming from the database
side ... funny ;-)

I have attached a small patch that solved the problem for me and might be a
start for you, but be aware, it's more a crutch than a solution.

- this patch won't make it into the project, so you have to maintain it on
  your own

- it doesn't try to work for the admin or for newforms (but made be working
  for it), so I didn't have to deal with the admin URLs at all.

- it does not deal with creating databases or database introspection.

For a model with multiple primary keys, add an Meta class attribute
`has_composite_primary_key_key=True`. Here's an example model with 
multiple primary keys:

class Kundemail(UpdatelogMixin, models.Model):
    kunde = models.ForeignKey(Kunde, db_column='kunde', primary_key = True)
    dienst = DescriptorField(Descr, "dienst", db_column="dienst", primary_key = 
True)
    person = models.ForeignKey(Person, db_column='person', primary_key = True)

    class Meta:
        db_table = 'kundemail'
        has_composite_primary_key = True

So long,

Michael

-- 
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company
 
Vorstand: Ingo Kraupa (Vorsitzender), Joachim Astel, Hansjochen Klenk - 
Vorsitzender des Aufsichtsrats: Stefan Schnabel - AG Nürnberg HRB 17689

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

>From nobody Mon Sep 17 00:00:00 2001
From: Michael Radziej <[EMAIL PROTECTED]>
Date: Mon Aug 13 16:03:06 2007 +0200
Subject: [PATCH] composite pkey

Refreshed patch composite-pkey.
(Base: da455ae19930af1b4513b37de8bd8e9f68ec6c3d)
(Last: 22c5ecf0407989824916f6b1676a4632c336a9d1)

---

 django/core/management.py   |    9 ++++++++-
 django/db/models/options.py |    4 +++-
 2 files changed, 11 insertions(+), 2 deletions(-)

base 0f7580c29c7e992e9a071d3df62260e0609c2c68
last f7b6a55742bbf1ff867d841cdc17942fff357470
diff --git a/django/core/management.py b/django/core/management.py
index 882ff6de323a45a718c4d84b592da8a1ecc7357e..7f11101e69490faca4dcaac1b6acd29454a6ab0b 100644
--- a/django/core/management.py
+++ b/django/core/management.py
@@ -157,6 +157,7 @@ def _get_sql_model_create(model, known_models=set()):
     opts = model._meta
     final_output = []
     table_output = []
+    composite_pkeys = []
     pending_references = {}
     for f in opts.fields:
         col_type = f.db_type()
@@ -172,7 +173,10 @@ def _get_sql_model_create(model, known_models=set()):
         if f.unique and (not f.primary_key or backend.allows_unique_and_pk):
             field_output.append(style.SQL_KEYWORD('UNIQUE'))
         if f.primary_key:
-            field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
+            if opts.has_composite_primary_key:
+                composite_pkeys.append(f)
+            else:
+                field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
         if tablespace and backend.supports_tablespaces and (f.unique or f.primary_key) and backend.autoindexes_primary_keys:
             # We must specify the index tablespace inline, because we
             # won't be generating a CREATE INDEX statement for this field.
@@ -196,6 +200,9 @@ def _get_sql_model_create(model, known_models=set()):
     for field_constraints in opts.unique_together:
         table_output.append(style.SQL_KEYWORD('UNIQUE') + ' (%s)' % \
             ", ".join([backend.quote_name(style.SQL_FIELD(opts.get_field(f).column)) for f in field_constraints]))
+    if opts.has_composite_primary_key:
+        table_output.append(style.SQL_KEYWORD('PRIMARY KEY')+ ' (%s)' % \
+            ", ".join([backend.quote_name(style.SQL_FIELD(f.column)) for f in composite_pkeys]))
 
     full_statement = [style.SQL_KEYWORD('CREATE TABLE') + ' ' + style.SQL_TABLE(backend.quote_name(opts.db_table)) + ' (']
     for i, line in enumerate(table_output): # Combine and add commas.
diff --git a/django/db/models/options.py b/django/db/models/options.py
index 7cccb611cf354d5e9d5288a05f7c788ee6a4fa52..60e050b2ddd3a26a4941ba5fb1706338699137a7 100644
--- a/django/db/models/options.py
+++ b/django/db/models/options.py
@@ -15,7 +15,8 @@ get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|
 
 DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
                  'unique_together', 'permissions', 'get_latest_by',
-                 'order_with_respect_to', 'app_label', 'db_tablespace')
+                 'order_with_respect_to', 'app_label', 'db_tablespace',
+                 'has_composite_primary_key')
 
 class Options(object):
     def __init__(self, meta):
@@ -36,6 +37,7 @@ class Options(object):
         self.has_auto_field = False
         self.one_to_one_field = None
         self.parents = []
+        self.has_composite_primary_key = False
 
     def contribute_to_class(self, cls, name):
         cls._meta = self
-- 
1.4.4.2

Reply via email to