﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
19385	Add support for multiple-column join	cseibert	nobody	"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.

{{{#!sql
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. 

https://github.com/jtillman/django/tree/MultiColumnJoin

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. 

{{{#!python
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."	New feature	closed	Database layer (models, ORM)	1.4	Normal	fixed	join		Accepted	1	0	0	1	0	0
