tags:

views:

127

answers:

2

I'm using a hand built (Postgres) database with Django. With "inspectdb" I was able to automatically create a model for it. The problem is that some tables have multiple primary keys (for many-to-many relations) and they are not accessible via Django.

What's the best way to access these tables?

+4  A: 

There is no way to use composite primary keys in Django's ORM as of now (up to v1.0.2).

I can only think of three solutions/workarounds:

  1. There is a fork of django with a composite pk patch at github that you might want to try.
  2. You could use SQLAlchemy together with Django.
  3. You have to add a single field primary key field to those tables.
Haes
+1: Fix your database -- there shouldn't be multiple primary keys in any table.
S.Lott
@S.Lott: Mmm, that kind of comment is just asking to stir up the whole natural vs. surrogate keys debate.
R. Bemrose
option 3 is probably the easiest... it's an old database anyway
Jack Ha
A: 

Django does have support for many-to-many relationships. If you want to use a helper table to manage this relationships, the ManyToManyField takes a through argument which specifies the table to use. You can't model anything terribly complex this way, but it is good for most simple applications.

Jason Baker