SQL Alchemy 跨星模式查询

2024-10-17 10:30:47 发布

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

我试图使用SQL炼金术映射一个现有的星型模式数据库,但在构建连接时遇到了困难。我已经阅读了文档和堆栈溢出,但没有找到一个明确的答案。在

这是我的课程的简单布局。学校和学生是具有上下文信息的不同维表。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数据帧中。有人能提供一个清楚的例子吗?在

谢谢!在


Tags: keytruebasestringcolumnintegerstudent学生
1条回答
网友
1楼 · 发布于 2024-10-17 10:30:47

对于多对多关系,请将secondary=...参数用于relationship(),并将其指向关联表。看到了吗 http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#many-to-many以获取完整的示例。在

如果连接表包含额外的数据,带有关联对象和关联代理的更高级模式使客户端代码相对直接。请参阅http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html以获取与原问题中所述情况类似的示例。在

相关问题 更多 >