4
例如,如何从SQLAlchemy中的对象访问相关的ForeignKey对象?
我有一个对象映射到一个表。 IE:
location = db.Column(db.Integer, db.ForeignKey('location.id'))
当我做object.location,我得到的实际foreignkey值。但我不想这样,我怎么才能获得对象(就像在Django ORM中)。谢谢!
例如,如何从SQLAlchemy中的对象访问相关的ForeignKey对象?
我有一个对象映射到一个表。 IE:
location = db.Column(db.Integer, db.ForeignKey('location.id'))
当我做object.location,我得到的实际foreignkey值。但我不想这样,我怎么才能获得对象(就像在Django ORM中)。谢谢!
如果你使用的声明基本对象(如果你想让它更喜欢Django的推荐),则:
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'))
child = relationship("Child", backref="parents")
真棒,谢谢! – nubela
如果这是您正在寻找的答案,您可以“接受”它吗? – six8
看起来你不需要第一个** Parent ** –