简体   繁体   English

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

[英]sqlalchemy: How can I associate object of other table with primary key?

I have a Transaction table as 我有一个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())

and Category table as Category表为

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

I add the transaction dataset as 我将交易数据集添加为

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

and I can see them as well when I query them 当我查询它们时,我也可以看到它们

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

and I see the data as well 我也看到了数据

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)>

But I do see the categoty_id as 62 and 77 . 但是我确实看到categoty_id6277

All I need the their values as objects like 我只需要它们的值作为对象

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)>

How can I attach the objects of different table? 如何附加不同表的对象?

I am sure just using primary_key will not suffice, but what else I need to do? 我确定仅使用primary_key还是不够的,但是我还需要做什么? Please advice 请指教

I have just started learning about SQLAlchemy 我刚刚开始学习SQLAlchemy

You probably need to create a relationship() : http://docs.sqlalchemy.org/en/latest/orm/relationships.html 您可能需要创建一个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")

I'm not sure if I entirely understood your question, but this should attach a transactions attribute to each category containing a collection of associated items. 我不确定我是否完全理解您的问题,但这应该将transactions属性附加到包含关联项目集合的每个类别。 Or alternatively: 或者:

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")

to attach objects many-to-one in the opposite direction (as @daydreamer already figured out). 以相反的方向多对一地附加对象(如@daydreamer已经弄清楚的)。

After adding the following line in my Transaction , I was able to refer to the Category associated with the transaction 在我的Transaction添加以下行后,我能够引用与该交易相关联的Category

category = relationship('Category')

My class Transaction finally looks like 我的班级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')

and I can see the output as 我可以看到输出为

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)>

Thanks to @David Marx for pointing to the right documentation 感谢@David Marx指出正确的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM