Python 查询多个表sqlalchemy
我试图跨4个表进行查询。它们都由外键绑定,并且都定义了关系。我面临的问题是,我似乎无法获得我所追求的价值 我有4张桌子: TDeviceReport:Python 查询多个表sqlalchemy,python,sqlalchemy,pyramid,Python,Sqlalchemy,Pyramid,我试图跨4个表进行查询。它们都由外键绑定,并且都定义了关系。我面临的问题是,我似乎无法获得我所追求的价值 我有4张桌子: TDeviceReport: ixDeviceReport(int, primary key) ixDeviceType(int, foreign key) ixReportType(int, foreign key) TReportType: ixReportType(int, primary key) sReportType(string) 运输部 i
ixDeviceReport(int, primary key)
ixDeviceType(int, foreign key)
ixReportType(int, foreign key)
TReportType:
ixReportType(int, primary key)
sReportType(string)
运输部
ixReportSection(int, primary key)
ixReportType(int, foreign key)
ixSection(int, foreign key)
和t部分:
ixSection(int,主键)
S节(字符串)
我正在尝试获取属于设备类型ID
的所有节名(t节.s节
)。问题是我对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',)------------------------------------------
您可以尝试此
打印节.t节.s节
'“节”应该是一个实例,在这种情况下,它是有效的。如果这不起作用,那么您的查询和连接内容是错误的,在这种情况下,这将有助于:
你没有告诉我它打印什么:
print”部分:“+str(部分)
。如果您能告诉我,如果它是空的,那么您的查询是错误的,这将非常有帮助。嘿,谢谢您的回答。我稍微更改了我的查询(将以编辑方式发布)。现在它为我的眼睛打印了一些看起来仍然有点滑稽的部分(或者至少看起来是这样),这解决了你的问题吗?目前只有这个连接对我有意义。join(TDeviceReport)“它似乎在打印正确的信息。我会进一步调查的