如何使用声明式创建一对一关系 [英] How to create one-to-one relationships with declarative
本文介绍了如何使用声明式创建一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表,foo
和 bar
,我希望 foo.bar_id
链接到 bar
.问题在于,这是一种单向的一对一关系.bar
必须对 foo
一无所知.对于每个 foo,将只有一个 bar
.
I have two tables, foo
and bar
, and I want foo.bar_id
to link to bar
. The catch is that this is a one-way one-to-one relationship. bar
must not know anything about foo
. For every foo, there will be one and only one bar
.
理想情况下,选择 foo 后,我可以执行以下操作:
Ideally, after selecting a foo, I could do something like this:
myfoo.bar.whatever = 5
如何做到这一点?
推荐答案
如果您想要真正的一对一关系,您还必须在关系定义中使用uselist=False".
If you want a true one-to-one relationship, you also have to use the "uselist=False" in your relationship definition.
bar_id = Column(Integer, ForeignKey(Bar.id))
bar = relationship(Bar, uselist=False)
这篇关于如何使用声明式创建一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文