backref 与 back_populates 区别

那如果我们需要得知 child 的 parent 对象呢?能不能直接访问 child.parent?

为了实现这个功能,SQLAlchemy 提供了 backref 和 back_populates 两个参数。

两个参数的效果完全一致,区别在于,backref 只需要在 Parent 类中声明 children,Child.parent 会被动态创建。

而 back_populates 必须在两个类中显式地使用 back_populates,更显繁琐。(但是也更清晰?)

你可能感兴趣的:(backref 与 back_populates 区别)