Awesome q2a theme

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

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

class File(models.Model):
#some stuff

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











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

1 Answer

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

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,187 answers
40,796 users