Hi Francesco,
I had a similar issue the other day, and came up with a solution of
sorts - but will be interested to hear from people with more Django
experience as to whether what I've done is a good idea.

If we your example to:

class MyModel(models.Model):
    field1 = ForeignKey('table1', db_index=True)
    field2 = ForeignKey('table2', db_index=True)

   class Meta:
        unique_together = (('field1', 'field2'),)

Then from my understanding, you should end up with a table for MyModel
that has a primary key automatically generated by Django, but your two
fields will be indexed, and you will not be able to have two records
where the combination of field1 and field2 is the same. So it has the
properties that I needed in the first place.

Does that help? Or, more importantly, can some one tell us whether
it's a bad idea?

Thanks in advance!
-Michael

Note: I think if you add the unique_together clause (or the db_index
for that matter), you'll need to either (1) recreate your database
tables (manage.py reset appname) which will destroy all your data, or
(2) modify your SQL tables manually to add the indexes and unique
fields.

On Mar 25, 10:07 am, "Russell Keith-Magee" <[EMAIL PROTECTED]>
wrote:
> On 3/25/07, checco <[EMAIL PROTECTED]> wrote:
>
>
>
> > and I want to create a primary key that is composed of field1 and
> > field 2 in table 3, is this possible in django? Can I do something
> > like...
>
> > field1 = ForeignKey('table1', primarykey=True)
> > field2= ForeignKey('table2', primarykey=True)
>
> Hi Francesco,
>
> You can make any individual field the primary key using
> `primary_key=True` - so, for example:
>
> class MyModel(models.Model):
>     key = ForeignKey('table1', primary_key=True)
>     data = ...
>
> would make a foreign key on Table1 the primary key on MyModel.
> However, if you give multiple fields the primary_key attribute, only
> the first will be used to designate a primary key. Django does not
> currently support multiple field primary keys.
>
> Yours,
> Russ Magee %-)


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to