It's be nice to do away with the need for the "pks" attribute, since it
provides more than one way to do something. Yes, it means that sometimes
you might need to test whether pk is a list or tuple, but that should be
relatively rare.
> - pk='hello' would still work as it does now
> - pk=('hello,') would work
> - pk=dict(field='hello') would work (to allow for unordered pk lists)
> - Composite primary keys would be created simply by passing
> primary_key=True as an argument on more than one field in your model.
This won't work nicely, since it doesn't allow you to specify the order
that will be used when assigning. Instead, for multi-column primary
keys, I'd suggest specifying them as an attribute on Meta:
class Meta:
primary_keys = ('username', 'location')
The order given there is the order used to specify them in assignments.
Having to reorder fields in a model to do that otherwise (if you only
used the primary_key attribute on the field) sets a kind of dangerous
precedence, since it says that might be an okay approach, except nothing
else can do it because ordering is then reserved for primary keys. We
already have a few issues that arise out of managers being treated
differently according to their order of declaration and it would be nice
to avoid that in this new feature.
> Anyone have any feedback, or started working on any code?
I've got some code that is going to land in the next week (before the
sprint, most likely) that adds most of the necessary support for
multicolumn fields. This is primarily to fix a few bugs with generic
relations in a non-specific way (it will work for all similar fields).
The stuff I'd done on multi-pk work -- which I'm not going to finish
before 1.0, because it's not that critical, so you're not duplicating
anything I'm doing there -- seemed to work nicely on top of that. So you
might want to keep an eye out for that. The main case where it's
applicable is when somebody does filter(foo__pk=(1, 2, 3)) because you
can no longer just replace "pk" with the name of the real attribute,
instead you have to treat it as multiple columns.
Regards,
Malcolm
I don't see anything too wrong with a tuple. It's a multi-valued
attribute. Remember that this is going to be a relatively rare case, so
it's not like everybody's code will be scattered about with these
tuples. Explicit db_column setting is rare anyway, doubly so for a
composite primary key.
>
> 2) should the composite keys be fieldname_pkfieldname, e.g.
>
> class Hi:
> char = CharField(primary_key=True)
>
> class Model1:
> key1 = ForeignKey(Hi)
> key2 = IntField()
>
> class Meta:
> primary_keys = ('key1', 'key2')
By the way, I made a typo earlier. This attribute should really be a
singular noun. You only have one primary key on a model (by definition).
It has multiple columns in this case, but it's only a single key.
>
> class Model2:
> fkey = ForeignKey(Model1)
>
> Should model2 have a schema of:
>
> id (INT)
> fkey_key1_id (CHARFIELD)
> fkey_key2 (INT)
It has to, doesn't it? What's the alternative you're thinking of.
At this early point in the process, it's probably worth just making it
work with something reasonable like this and not worrying about it being
perfect first time. At that point, we can then do a more detailed review
and some things might seem better another way. As with the other big
design things people are proposing, the timing's a little unfortunate at
the moment just because all my spare cycles and then some are going
towards the 1.0 release, so I can look at things quickly (and I've
thought a lot about this particular problem in the past), but I'm not
going to be able to spend a few hours thinking about the tricky corners
for a while.
Nothing you've proposed so far looks insane, so I'd be tempted to run
with it and see what you can build.
Regards,
Malcolm
Any progress on this patch David? I would be happy to take a look at
whatever you have and perhaps help out with completing the patch.
> Really I'm stuck at an architectural point.
>
> I have database validation and synchronization done, and the admin
> is working.
>
> What is left is more or less handling relatedfield lookups. The
> issue is, that field's are designed to reference more than one
> field, so it's a tough design deicision to make on how that should
> be approached.
I think the best (and the only one right) solution involves adding
multicolumn fields to Django and doing lookups with some syntax like
Model.objects.get(pk=('foo', 1)). There are other hackish approaches,
like using hash(tuple(pk[0], pk[1], ..., pk[n])) as foreign key, that
could work. However, I won't rely on them, since I'm not sure if
hash() implementation is guaranteed to be kept as is.
On other related point, what's the status of multicolumn fields?
Regards,
Alberto
To be clear, the syntax is:
myfkey = models.ForeignKey(SomeClass,to_field="id")
Take notes. There's going to be a lot going on at DjangoCon (including
celebrating), so there will be a group who are putting off thinking
about this until quieten down who'll likely be busy throughout the
weekend. In a few weeks we can start looking at this more concretely on
the list and working out how it might fit into the whole.
Regards,
Malcolm
No question. And for that, we have this very handy mailing list with
archives that will store all the discussion. Meanwhile, this weekend, we
have a two day conference with a very full schedule, no scheduled
downtime and a limited attendance group. This is all at the end of a
long process of nothing but dawn to dusk bug fixing for a group of us,
so some of us will be relaxing. So by all means have a conversation with
some people, that's a given. But you'll still need to bring the results
to the mailing list in a coherent format so that it can be looked at
when we start to consider the patch.
This is a serious proposal. It's being taken seriously. But realise the
broader things going on as well. If you're hoping/expecting to get this
resolved in a couple of days, you are, frankly, optimistic.
Regards,
Malcolm
It allows you to use them, automatically creates them, and has some of the admin handling done. However, there's still no API design around multi-column fields (no one seems to want to talk about it) so I'm pretty much stopped working on it.
e.g. You can't say field1 = this, field2 = that, and then say compositekey = field1,field2 you instead are forced to do key1=blah, key2=blah in all your lookups, and no easy foreignkey properties.
I'm running this on production environments, so it works fine, but I can up SVN and fix any conflicts and post a patch again.