0

我有一张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_id6277

我只需要它们的值作为对象

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

4

2 回答 2

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 已经弄清楚的那样)。

于 2013-03-10T02:00:18.050 回答
0

在 my 中添加以下行后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 指出正确的文档

于 2013-03-10T02:09:43.073 回答