Add support for multiple-column join
|Reported by:||cseibert||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
For enterprise multi-tenant databases, a typical table schema might have both company_id and record_id, where record_id is only unique within that company_id. There is already an issue open for multi-column primary keys; #373 (ticket).
Even without support for multi-column primary key, it would be useful to be able to do multi-column joins, so we can group records in the same company on the same page in the index.
SELECT * FROM app_record a JOIN app_user b ON b.id = a.user_id AND b.company_id = a.company_id;
My team is in the process of writing such a patch for Django 1.4.
In this patch, the ForeignKey field accepts an additional parameter named 'include_related'. This allows you to dictate what fields are related to each other on a model. It takes a list of tuples which contains the left and right hand property names.
class User(models.Model): name = models.CharField(max_length=128) company = models.ForeignKey(Company) class Record(models.Model): name = models.CharField(max_length=128) company = models.ForeignKey(Company) user = models.ForeignKey(User, include_related=[('company', 'company')])
There are some situations that are not implemented yet, such as pre-fetch.
We're looking for feedback on the idea, feedback on the implementation and feedback on how to structure the patch to maximize palpability to the core devs. We realize that this is unlikely to be merged.
Change History (36)
comment:1 Changed 3 years ago by akaariai
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:32 Changed 2 years ago by akaariai
- Resolution set to fixed
- Status changed from new to closed