我有以下SQLAlchemy映射的类:
class TagValue(Model):
tag_value_id = Column(Integer, primary_key=True)
tag_value_name = Column(String(255))
def __repr__(self):
return '{}'.format(self.tag_value_name)
tag_values_table = Table('tag_values', Model.metadata,
Column('tag_id', Integer, ForeignKey('tag.tag_id'),
primary_key=True),
Column('tag_value_id', Integer,
ForeignKey('tag_value.tag_value_id'), primary_key=True))
class Tag(Model):
tag_id = Column(Integer, primary_key=True)
tag_name = Column(String(255))
tag_value = relationship("TagValue", secondary=lambda:
tag_values_table, backref="tag")
def __repr__(self):
return '{}'.format(self.tag_name)
表视图如下:
从标记中选择*;
tag_id | tag_name
--------+-------------
1 | Lever
2 | Department
3 | Data Source
4 | Category
5 | Term
6 | Where Used
从tag_value中选择*;
tag_value_id | tag_value_name
--------------+------------------
1 | Engage
2 | Convert
3 | Deliver
4 | Retain
5 | Marketing
6 | Sales
7 | Operations
8 | Finance
从tag_values中选择*;
tag_id | tag_value_id
--------+--------------
1 | 1
1 | 2
1 | 3
1 | 4
2 | 5
2 | 6
2 | 7
2 | 8
如何使用session.query
与这三个表进行联接,以使结果查询返回以下表
tag_name | tag_value_name
-------------+------------------
Lever | Engage
Lever | Convert
Lever | Deliver
Lever | Retain
Department | Marketing
Department | Sales
Department | Operations
Department | Finance
我已经使用了上面的表格:
select t.tag_name,tv.tag_value_name from tag_values tvs join tag t
on t.tag_id = tvs.tag_id join tag_value tv on tv.tag_value_id =
tvs.tag_value_id;
如何使用session.query
获得相同的条件?