Sqlalchemy的关系
我使用sqlalchemy orm和pydantic
我有以下简单的桌子
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True, nullable=False)
children = relationship(
"Child",
backref="parent",
uselist=True
)
class Child(Base):
__tablename__ = 'children'
id = Column(Integer, primary_key=True, nullable=False)
parent.id = Column(Integer, ForeignKey(Parent.id))
age = Column(Integer)
让父母和他们的孩子很容易
但是我该如何随着年龄的年龄过滤?
如果您得到以下内容,然后与Pydantic序列化,所有孩子都将嵌套
parents = db.query(Parent).all()
I Use sqlalchemy ORM and pydantic
I have the following simple table
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True, nullable=False)
children = relationship(
"Child",
backref="parent",
uselist=True
)
class Child(Base):
__tablename__ = 'children'
id = Column(Integer, primary_key=True, nullable=False)
parent.id = Column(Integer, ForeignKey(Parent.id))
age = Column(Integer)
Getting the parents and their children is easy
But how can i filter that child with age?
If you get the following and then serialize it with pydantic, all children will be nested
parents = db.query(Parent).all()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在父桌和子桌之间进行连接,以便对孩子进行过滤器。
You need to do a join between your parent table and your child table to do a filter on the child.