admin管理员组文章数量:1319479
I am using django to interface with an existing database that I have no control over.
The database frequently uses multiple columns for making relations and has uniqueness over all of those columns.
The django inspectdb
model generator correctly identified these as unique_together = (('foo', 'bar'), ('foo', 'bar'),)
, resulting in the following model:
class A(models.Model):
foo = models.IntegerField(db_column='FOO', primary_key=True)
bar = models.IntegerField(db_column='BAR')
class Meta:
managed = False
db_table = 'A'
unique_together = (('foo', 'bar'), ('foo', 'bar'),)
Now there is another table whose entries relate to that table on both columns. When querying using SQL I would relate them like this:
SELECT * FROM A LEFT JOIN B ON A.foo = B.foo AND A.bar = B.bar
However inspectdb
failed to correctly map the two columns being used in a relating table:
class B(models.Model):
foo = models.OneToOneField(A, models.DO_NOTHING, db_column='A', primary_key=True)
bar = models.ForeignKey(A, models.DO_NOTHING, db_column='A')
The above is what it generated, however in reality it is a many-to-one relationship to table A. This then causes an error at runtime because the column bar
in table A
is not unique.
Can I somehow define this relationship in a django model?
And how do I query this efficiently so that django generates a JOIN
expression with two conditionals?
I am using django to interface with an existing database that I have no control over.
The database frequently uses multiple columns for making relations and has uniqueness over all of those columns.
The django inspectdb
model generator correctly identified these as unique_together = (('foo', 'bar'), ('foo', 'bar'),)
, resulting in the following model:
class A(models.Model):
foo = models.IntegerField(db_column='FOO', primary_key=True)
bar = models.IntegerField(db_column='BAR')
class Meta:
managed = False
db_table = 'A'
unique_together = (('foo', 'bar'), ('foo', 'bar'),)
Now there is another table whose entries relate to that table on both columns. When querying using SQL I would relate them like this:
SELECT * FROM A LEFT JOIN B ON A.foo = B.foo AND A.bar = B.bar
However inspectdb
failed to correctly map the two columns being used in a relating table:
class B(models.Model):
foo = models.OneToOneField(A, models.DO_NOTHING, db_column='A', primary_key=True)
bar = models.ForeignKey(A, models.DO_NOTHING, db_column='A')
The above is what it generated, however in reality it is a many-to-one relationship to table A. This then causes an error at runtime because the column bar
in table A
is not unique.
Can I somehow define this relationship in a django model?
And how do I query this efficiently so that django generates a JOIN
expression with two conditionals?
- No, Django does not allow keys to be composite ones, nor Foreign keys to link to multiple ones. – willeM_ Van Onsem Commented Jan 19 at 21:02
1 Answer
Reset to default 1Composite keys cannot be related to in Django. Check the ForeignObject
mention in that article however, but note the explicit warning given there.
You can probably create a dirty workaround to get your fetch working in a relatively straightforward way, but it won't retain the ORM's relationship management:
class B(models.Model):
foo = models.IntegerField()
bar = models.IntegerField()
b = B.objects.first()
a = A.objects.filter(foo=b.foo, bar=b.bar)
本文标签: Django relation on two database columnsStack Overflow
版权声明:本文标题:Django relation on two database columns - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742059779a2418509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论