How to get rid of duplicate joins s at the intersecting ForeignKey in Django?

Noticed one unpleasant thing. Suppose there is a model that linked the other two have the same ForeignKey.

class File(models.Model):<br>
 #some stuff<br>
<br / >
class ServerFile(models.Model):<br>
 <b>file = models.ForeignKey('File')</b><br>
 #some stuff<br>
<br / >
class UserFile(models.Model):<br>
 <b>file = models.ForeignKey('File')</b><br>
 #some stuff<br>
<br / >
class Link(models.Model): <br>
 user_file = models.ForeignKey('UserFile')<br>
 server_file = models.ForeignKey('ServerFile')<br>
 #some stuff

Accordingly, when included list_select_related, get an additional join File











FROM `fff_link`

INNER JOIN `fff_userfile`

ON (`fff_link`.`user_file_id` = `fff_userfile`.`id`)

  INNER JOIN `fff_file`

ON (`fff_userfile`.`file_id` = `fff_file`.`id`)

INNER JOIN `fff_serverfile`

ON (`fff_link`.`server_file_id` = `fff_serverfile`.`id`)

  INNER JOIN `fff_file` T5

ON (`fff_serverfile`.`file_id` = T5.`id`)

Can anyone come across? How to treat?

The rejection of the intersection is not an option, of course
October 14th 19 at 11:33
1 answer
October 14th 19 at 11:35
But why treat? In this case, the JOIN is absolutely adequate.
This greatly simplified example. On one such table 2 repeat the request for a large amount of data cause wild brakes. - Darion.Daniel commented on October 14th 19 at 11:38
Well, how would you write a query with a single JOIN-om file? You do need to pull two different file object for a single object link.
Afraid to capitanich, but everything was in order with indexes? - deja_Mayert83 commented on October 14th 19 at 11:41
Indexes on foreign keys automatically generalise, EXPLAIN to them showed.
The fact of the matter is that file the same and other way to group objects is not. It is to say the different incarnations of the same object. It sees the file as the user, and where it is real. - Darion.Daniel commented on October 14th 19 at 11:44
Add another link to the model. - deja_Mayert83 commented on October 14th 19 at 11:47

Find more questions by tags ORMSQLDjango