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 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
signature.asc
Description: Digital signature