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