On Sun, 2007-03-25 at 10:52 +0000, Michael wrote: > 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?
This certainly works and is the currently recommended way to have multi-column uniqueness at the moment. It's not 100% equivalent to multi-column primary keys, though, but the differences generally only arise when you're trying to use Django over an existing database. We might be able to fix this problem one day, although the current real difficulty is how to address them in the admin interface -- there hasn't yet been any good ideas that don't make existing admin URLs ugly. [Aside: If you're wondering why this is hard, consider a model in Django that has a custom primary key and an instance with a key value of 'a/b/c', now try to make an admin URL from that and another column without having to somehow put escaping on every slash in sight, which we don't need to do at the moment.] Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---