【发布时间】:2015-06-04 02:18:02
【问题描述】:
我正在尝试使用 SQL alchemy 映射现有的星型模式数据库,但在构建联接时遇到了麻烦。我已经阅读了文档和堆栈溢出,但没有找到明确的答案。
这是我的课程的简单布局。 School 和 Student 是具有上下文信息的不同维度表。 StudentScore 是一个多对多关联表的事实表,每个学生评估都有一行。
class School(Base):
__tablename__ = 'DimSchool'
id = Column('SchoolKey', Integer, primary_key=True)
name = Column('SchoolName', String)
district = Column('SchoolDistrict', String)
class Student(Base):
__tablename__ = 'DimStudent'
id = Column('StudentKey',Integer, primary_key=True)
srcstudentid = ('SrcStudentId', Integer)
firstname = Column('FirstName', String)
middlename = Column('MiddleName', String)
lastname = Column('LastName', String)
class StudentScore(Base):
__tablename__ = 'FactStudentScore'
StudentKey = Column('StudentKey', Integer, ForeignKey('DimStudent.StudentKey'), primary_key = True)
SchoolKey = Column('SchoolKey', Integer, ForeignKey('DimSchool.SchoolKey'), primary_key = True)
PointsPossible = Column('PointsPossible', Integer)
PointsReceived = Column('PointsReceived', Integer)
student = relationship("Student", backref='studentscore')
school = relationship("School", backref='studentscore')
我正在尝试查询给定的学校,加入 StudentScore 和 Student 以返回每个学生的一行,以及他们的分数和学校关联。最终,我会将其放入 pandas 数据框中。有人可以提供一个明确的例子来说明如何做到这一点吗?
谢谢!
【问题讨论】:
标签: python sqlalchemy