查询多表sqlalchemy

2024-09-29 23:17:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图跨4个表进行查询。它们都由外键绑定,并且都定义了关系。我现在的问题是我似乎无法得到我所追求的价值。在

我有4张桌子:

t设备报告:

ixDeviceReport(int, primary key) 
ixDeviceType(int, foreign key)   
ixReportType(int, foreign key)

t端口类型:

^{pr2}$

TReportSection公司

ixReportSection(int, primary key)          
ixReportType(int, foreign key)
ixSection(int, foreign key)

还有t节: ixSection(int,主键) S节(字符串)

我正在尝试获取属于deviceTypeID的所有节名(TSection.sSection)。问题是我对sqlAlchemy还不太熟悉,不知道该怎么做。我没试着去查结果。在

for section in DBSession.query(TDeviceReport, TReportType, TReportSection, TSection).join(TReportType).join(TReportSection).join(TSection).filter(TDeviceReport.ixDeviceType==deviceTypeID).all():
    print "------------------------------------------------"
    print "section : " + str(section )

如有任何指导,将不胜感激

编辑 我重新编辑了一下查询,这是它打印的结果

#get section headers
sections = []
for section in DBSession.query(TSection.sSection).join(TReportSection).join(TReportType).join(TDeviceReport).filter(TDeviceReport.ixDeviceType==deviceTypeID).all():
    sections.append(str(section[0]))
    print "section: " + str(section) + "------------------------------------------"

打印结果:

section: (u'Trip Unit Results',)------------------------------------------
section: (u'Generic Header',)------------------------------------------
section: (u'Circuit Breaker Data',)------------------------------------------
section: (u'Trip Unit Data',)------------------------------------------
section: (u'Sensor Data',)------------------------------------------
section: (u'Insulation Resistance',)------------------------------------------
section: (u'Contact Resistance',)------------------------------------------
section: (u'Breaker Inspection',)------------------------------------------
section: (u'Cell Inspection',)------------------------------------------

Tags: keydatasectionintprintjoinforeignprimary
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:15

你可以试试这个print section.TSection.sSection在这种情况下,应该是“工程”部分。如果这不起作用,那么您的查询和连接是错误的,在这种情况下这将有帮助:http://docs.sqlalchemy.org/en/rel_0_9/orm/query.html

你没告诉我那印的是什么:print "section : " + str(section )。如果你能告诉我,如果它是空的,那么你的查询就错了。在

相关问题 更多 >

    热门问题