A Deduplication DB schema with sqlalchemy - 如何用 ORM 语义表示组?
我正在尝试使用 mysql 创建实体重复数据删除模式的简单表示,并使用 sqlalchemy 进行编程访问。
我正在尝试实现一种特定的效果,我认为这是一种自引用查询,但我不确定:
本质上我有一个“实体”表(具有唯一的entity_id)和一个关联的实体对象, 然后是一个entity_groups表,为了简单起见,它有一个“group_id”和“entity_id”列,这样我就可以通过为该关系创建一行来向组“注册”一个实体。 该表也与 ORM 对象 - EntityGroup 关联。
问题是,如何获取 EntityGroup 对象引用组中的所有实体?
我希望我需要写一些类似的东西:
mapper(EntityGroup,entity_groups_table, 属性={ “实体”:关系( 实体, ....? ) },
我对细节有点模糊。基本上我需要entity_groups 中与对象表示的行具有相同group_id 的所有行。然后我需要实现 与这些行的entity_id 列关联的所有实体对象。这听起来像是可以通过 sqlalchemy 中更详细的 Query() 操作来实现,但我不确定如何将其与关系()构造结合起来(如果有的话 - 也许需要手动?)
任何帮助都会有用,我希望我说得很清楚并且切中要点
I'm trying to create a simple representation for an entity deduplication schema using mysql, and using sqlalchemy for programmatic access.
I'm trying to achieve a specific effect which I think is kind of a self-referential query but i'm not sure:
Essentially I have an 'entities' table (with unique entity_id) and an associated Entity object,
and then an entity_groups table which (for simplicity) has a 'group_id' and 'entity_id' columns, so that I 'register' an entity with a group by creating a row for that relation.
this table too is associated with an ORM object - EntityGroup.
Question is, how do i get the EntityGroup object reference all entities in the group?
I expect I need to write something like:
mapper(EntityGroup, entity_groups_table,
properties={
'entities': relationship(
Entity,
.... ?
)
},
and i'm alittle fuzzy on the details. Basically I need all the rows in entity_groups that have the same group_id as the row represented by the object. And then I need to materialize
all the Entity objects associated those rows' entity_id column. This sounds like something achievable by a more verbose Query() operation in sqlalchemy, but i'm not sure how to combine that with the relationship() construct (if at all - perhaps go manual? )
Any help will be useful, I hope I was clear and to the point
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实不应该使用
查询
来执行此操作,就好像您正确配置了关系一样,您将自动获得此结果。假设您仅使用entity_group
表来存储关系而不做其他任何事情,您应该只配置 多对多 关系如记录。完全有效的示例应该有所帮助:应该产生类似以下内容:
You really should not do it using a
Query
, as if you configure the relationships properly you will get this automatically. Assuming that you useentity_group
table solely to store the relationship and nothing else, you should just configure many-to-many relationship as documented. Fully working example should help:should produce something like: