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

0 like 0 dislike
7 views
Noticed one unpleasant thing. Suppose there is a model that linked the other two have the same ForeignKey.

class File(models.Model):
#some stuff
pass

class ServerFile(models.Model):
    file = models.ForeignKey('File')
#some stuff

class UserFile(models.Model):
    file = models.ForeignKey('File')
#some stuff

class Link(models.Model):
user_file = models.ForeignKey('UserFile')
server_file = models.ForeignKey('ServerFile')
#some stuff



Accordingly, when included list_select_related, get an additional join File


SELECT

`fff_link`.`id`,

`fff_link`.`user_file_id`,

`fff_link`.`server_file_id`,

`fff_userfile`.`id`,

`fff_userfile`.`file_id`,

`fff_file`.`id`,

`fff_serverfile`.`id`,

`fff_serverfile`.`file_id`,

T5.`id`

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
by | 7 views

1 Answer

0 like 0 dislike
But why treat? In this case, the JOIN is absolutely adequate.
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked May 20, 2019 by anttoon
110,608 questions
257,186 answers
0 comments
28,660 users