On Wed, 16 Mar 2011 07:49:17 +0200, Michal Petrucha <michal.petru...@ksp.sk> wrote:

On Tue, Mar 15, 2011 at 09:15:29PM -0500, Javier Guerra Giraldez wrote:
On Tue, Mar 15, 2011 at 7:14 PM, Christophe Pettus <x...@thebuild.com> wrote:
> A concern here is that composite indexes, like unique, are
> sensitive to the ordering of the fields, which means that the
> ordering of the fields in the class declaration becomes important.

This is the same reason a new Meta flag has been agreed upon in the
past. (That, however, does not mean it has to be that way.)

a simplistic proposal:

the order of the fields on a composite index is determined by the
exact value given to the primary_key argument.

that way, just setting primary_key=True on a few fields won't
guarantee order, but something like:

class City (models.Model):
    country = models.CharField (max_length=2, primary_key=1)
    state = models.CharField (max_length=2, primary_key=2)
    city = models.CharField (max_length=3, primary_key=2)
    Name = models.CharField (max_length=20)

would set the (country,state,city) primary key in the obvious order.

in short: giving any non-falsy value to primary_key would add the
field to the key, the exact value would determine ordering.

I like this proposal. It might even be easier to implement than
fiddling with new Meta flags and everything.

One minor detail though, just setting primary_key=True on multiple
fields would still have to guarantee some ordering since the primary
key would be represented by a tuple. If you don't know for sure in
which order the values are, you can't really use the .pk property.

This feels like something that wants a named-tuple (or a full blown dict). Alternatively, provide a method on the model class that takes the name=value arguments (queryset style) and returns the right pk tuple. Otherwise, the exact ordering of the fields in the pk tuple becomes yet another implicit(!) part of the model's contract - and any code that wants to use this model will be that much more brittle.


This would require a thorough explanation in the docs, that if you
supply values that compare equal to primary_key, the order of fields
will be the same as in the model definition.

Michal Petrucha

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

Reply via email to