我有两个外键,我需要在连接中同时连接两个字段。当我按照以下方式执行select_related时:
.select_related('key1', 'key2')
它只在其中一个键上进行连接而不是两个键。我把它们都声明为外键,related_names等于' +'和primary_key =模型中的True。
models.py中的模型:
class table1(models.Model):
key1 = models.ForeignKey('table2', db_column='key1', max_length=6, related_name='+', primary_key=True)
key2 = models.ForeignKey('table2', db_column='key2', max_length=4, related_name='+', primary_key=True)
class table2(models.Model):
key1 = models.Charfield(db_column='key1', max_length=6, related_name='+', primary_key=True)
key2 = models.Charfield(db_column='key2', max_length=4, related_name='+', primary_key=True)
正在发生的错误:
[table1] INNER JOIN [table2] ON ( [table1].[key1] = [table2 [key2] ) INNER JOIN [table2] T3 ON ( [table1].[key2] =T3.[key2] )
当它应该是key1 = key1和key2 = key2。