本文介绍了如何使用SQLAlchem​​y的建造许多一对多的关系:一个很好的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多建筑到多关系SQLAlchem​​y的文档和教程,但我无法弄清楚如何在关联表包含比2外键更做正确。

I have read the SQLAlchemy documentation and tutorial about building many-to-many relation but I could not figure out how to do it properly when the association table contains more than the 2 foreign keys.

我有一个项目表,每个项目都有很多细节。细节可以在许多项目是相同的,所以有项目和详情之间的多对多关系

I have a table of items and every item has many details. Details can be the same on many items, so there is a many-to-many relation between items and details

我有以下内容:

class Item(Base):
    __tablename__ = 'Item'
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    description = Column(Text)

class Detail(Base):
    __tablename__ = 'Detail'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    value = Column(String)

我的关联表(它在code其他2前定义):

My association table is (It's defined before the other 2 in the code):

class ItemDetail(Base):
    __tablename__ = 'ItemDetail'
    id = Column(Integer, primary_key=True)
    itemId = Column(Integer, ForeignKey('Item.id'))
    detailId = Column(Integer, ForeignKey('Detail.id'))
    endDate = Column(Date)

在文档中,它说我需要使用关联对象。我无法弄清楚如何正确地使用它,因为它是与映射的形式混合声明和例子似乎并不完整。我加了一行:

In the documentation, it's said that I need to use the "association object". I could not figure out how to use it properly, since it's mixed declarative with mapper forms and the examples seem not to be complete. I added the line:

details = relation(ItemDetail)

作为项目类的构件和线

as a member of Item class and the line:

itemDetail = relation('Detail')

作为关联表的成员,如文档中描述的。

as a member of the association table, as described in the documentation.

当我做项目= session.query(项目)。首先(),该item.details不详细的对象列表,但ItemDetail对象的列表。

when I do item = session.query(Item).first(), the item.details is not a list of Detail objects, but a list of ItemDetail objects.

如何能获得正确信息项的对象,即item.details应详细对象的列表?

How can I get details properly in Item objects, i.e., item.details should be a list of Detail objects?

推荐答案

从我看到你已经找到了答案的评论。但SQLAlchem​​y的文档是一个新用户相当热烈,我是用了同样的问题挣扎。所以,以供将来参考:

From the comments I see you've found the answer. But the SQLAlchemy documentation is quite overwhelming for a 'new user' and I was struggling with the same question. So for future reference:

ItemDetail = Table('ItemDetail',
    Column('id', Integer, primary_key=True),
    Column('itemId', Integer, ForeignKey('Item.id')),
    Column('detailId', Integer, ForeignKey('Detail.id')),
    Column('endDate', Date))

class Item(Base):
    __tablename__ = 'Item'
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    description = Column(Text)
    details = relationship('Detail', secondary=ItemDetail, backref='Item')

class Detail(Base):
    __tablename__ = 'Detail'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    value = Column(String)
    items = relationship('Item', secondary=ItemDetail, backref='Detail')

这篇关于如何使用SQLAlchem​​y的建造许多一对多的关系:一个很好的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:34