sqlalchemy:如何将其他表的对象与主键关联?

时间:2013-03-10 01:57:50

标签: python orm sqlalchemy

我有Transaction

class Transaction(Base):
    __tablename__ = 'transaction'
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String)
    amount = Column('amount', Float)
    category_id = Column('category_id', Integer, ForeignKey('category.id'))
    date_created = Column('date_created', Date, default=datetime.now())
    date_modified = Column('date_modifed', Date, onupdate=datetime.now())

Category表格为

class Category(Base):
    __tablename__ = 'category'
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String)
    subtype = Column('subtype', String)

我将交易数据集添加为

name, amount, category_id
Costo,59.0,5
Costo,20,5
Safeway,6.75,1
Safeway,11,1
Safeway,19,1

我查询时也可以看到它们

for transaction in session.query(Transaction):
    logging.info(transaction)

我也看到了数据

INFO:root:<Transaction(22, Medical Insurance, 110.0, 62, 2013-03-09, None)>
INFO:root:<Transaction(23, AllState Renter Insurance, 100.0, 77, 2013-03-09, None)>

但我确实将categoty_id视为 62 77

我只需要将它们的值作为像

这样的对象
INFO:root:<Transaction(22, Medical Insurance, 110.0, **Medical**, 2013-03-09, None)>
INFO:root:<Transaction(23, AllState Renter Insurance, 100.0, **Insurance**, 2013-03-09, None)>

如何附加不同表格的对象?

我确信只使用primary_key是不够的,但我还需要做什么? 请建议

我刚开始学习SQLAlchemy

2 个答案:

答案 0 :(得分:1)

您可能需要创建relationship()http://docs.sqlalchemy.org/en/latest/orm/relationships.html

from sqlalchemy.orm import relationship, backref

class Category(Base):
    __tablename__ = 'category'
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String)
    subtype = Column('subtype', String)
    # This line is new
    transactions = relationship("Transaction", backref="category")

我不确定我是否完全理解您的问题,但是这应该为每个包含相关项集合的类别附加transactions属性。或者:

class Transaction(Base):
    __tablename__ = 'transaction'
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String)
    amount = Column('amount', Float)
    category_id = Column('category_id', Integer, ForeignKey('category.id'))
    date_created = Column('date_created', Date, default=datetime.now())
    date_modified = Column('date_modifed', Date, onupdate=datetime.now())
    category = relationship("category")

以相反的方向多对一地附加对象(正如@daydreamer已经想到的那样)。

答案 1 :(得分:0)

在我的Transaction中添加以下行后,我可以参考与交易相关联的Category

category = relationship('Category')

我的班级Transaction最终看起来像

class Transaction(Base):
    __tablename__ = 'transaction'
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String)
    amount = Column('amount', Float)
    category_id = Column('category_id', Integer, ForeignKey('category.id'))
    date_created = Column('date_created', Date, default=datetime.now())
    date_modified = Column('date_modifed', Date, onupdate=datetime.now())
    category = relationship('Category')

我可以看到输出为

INFO:root:<Transaction(28, ATT, 89.0, <Category(148, Utilities, Phone)>, 2013-03-09, None)>
INFO:root:<Transaction(27, Car Gas, 47.0, <Category(146, Utilities, Gas & Fuel)>, 2013-03-09, None)>

感谢@David Marx指出正确的文档