python - SQLAlchemy 关联困惑

标签 python orm sqlalchemy

我在 SQLAlchemy 中有一个学生数据库,其中包含下表。

class Marks(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'marks'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    mark = Column(Integer, nullable=False, unique=False)
    subject_fk = Column(Integer, ForeignKey('subjects.id'))
    subject = relationship('Subjects', foreign_keys=[locale_fk],uselist=False,
                          backref='marks', innerjoin=False, post_update=False)


class Subjects(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'subjects'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    subject = Column(String(10, convert_unicode=True), nullable=False, unique=True)


class StudentIds(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'student_ids'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    student_id = Column(String(50, convert_unicode=True), nullable=False,unique=False)


    junctionid = relationship("Junction", cascade='save-update',
                                     backref='student_ids', lazy='select', post_update=True)
    mk=relationship("Marks",secondary=lambda:junction_table)
    marks = association_proxy('mk', 'mark')


class Junction(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = "junction_table"

    student_id_fk = Column(Integer, ForeignKey('student_ids.id'), primary_key=True)
    mark_fk = Column(Integer, ForeignKey('marks.id'), primary_key=True)
    mark = relationship('Marks', cascade='save-update', backref='mark_id_mapper', innerjoin=True)

我已在学生 ID、分数和科目表中填充数据。现在,我需要使用 student_idma​​rk 外键(Marks在每个主题中获得的数据都插入到标记表中)..如何完成此操作,我的意思是我可以在学生 ID 表中创建一个关联代理,该代理与标记表映射并在那里填充它?

我浏览了此文档:http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/associationproxy.html我有点明白我需要在student_id和marks之间建立这个代理..这样我就可以查询student_id.mark来获取分数。但我如何将其添加到我的连接表中呢?

有人可以指导我吗?

最佳答案

我建议这是一对多的关系(一个学生 - 许多分数, http://docs.sqlalchemy.org/en/rel_0_9/orm/basic_relationships.html#one-to-many )。因此,您可能希望将 Student_id 添加到 Marks 模型以及与 Marks 到 StudentIds 模型的关系,而不是创建 Junction 模型。

class StudentIds(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'student_ids'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    student_id = Column(String(50, convert_unicode=True), nullable=False,unique=False)


    junctionid = relationship("Junction", cascade='save-update',
                                 backref='student_ids', lazy='select', post_update=True)
    marks = relationship("Marks", backref="student")


class Marks(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'marks'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    mark = Column(Integer, nullable=False, unique=False)
    subject_fk = Column(Integer, ForeignKey('subjects.id'))
    subject = relationship('Subjects', foreign_keys=[subject_fk],uselist=False,
                      backref='marks', innerjoin=False, post_update=False)
    student_id = Column(Integer, ForeignKey('student_ids.id'))
    student = relationship('StudentIds', foreign_keys=[student_id])


class Subjects(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'subjects'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    subject = Column(String(10, convert_unicode=True), nullable=False, unique=True)

如果您想创建多对多关系,那么您可能需要考虑创建关联表( http://docs.sqlalchemy.org/en/rel_0_9/orm/basic_relationships.html#many-to-many )

association_table = Table('association', Base.metadata,
    Column('students_id', Integer, ForeignKey('student_ids.id')),
    Column('marks_id', Integer, ForeignKey('marks.id'))
)


class Marks(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'marks'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    mark = Column(Integer, nullable=False, unique=False)
    subject_fk = Column(Integer, ForeignKey('subjects.id'))
    subject = relationship('Subjects', foreign_keys=[subject_fk],uselist=False, backref='marks', innerjoin=False, post_update=False)


class Subjects(Base):
    __table_args__ = {'extend_existing': True}
    __tablename__ = 'subjects'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    subject = Column(String(10, convert_unicode=True), nullable=False, unique=True)


class StudentIds(Base):
   __table_args__ = {'extend_existing': True}
   __tablename__ = 'student_ids'
   id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
   student_id = Column(String(50, convert_unicode=True), nullable=False,unique=False)


   junctionid = relationship("Junction", cascade='save-update',
                                 backref='student_ids', lazy='select', post_update=True)
   marks = relationship("Marks", secondary=association_table)

无论如何,您不需要连接模型来创建学生与其分数之间的关系。

关于python - SQLAlchemy 关联困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28847921/

相关文章:

python - 获取不到一个月的所有元素

python - 在Python(Pandas/Numpy)中。如何创建具有两个独立系列的最大值/最小值的列?

python - 克服空数组的 ValueError

python - 使用 django orm 更新表而不删除关系

python - SQLAlchemy 中按特定格式的日期分组

Python 3 设置默认字节编码

django - 如何在 select_related 模型上进行注释?

python - Storm 或 SQLAlchemy ORM 是否允许从现有数据库创建模式?

python - sqlalchemy 多态多对多

python - 无法使用 SQLAlchemy 删除对象实例