SQLAlchemy 关联混乱

SQLAlchemy association confusion

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

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、分数和科目 table 中填充了数据。现在,我需要用 student_id[= 填充连接点 table 22=] 和 mark 外键(每个科目获得的分数插入到标记 table 中)..我如何完成这个,我的意思是我可以创建一个Student id table 内的关联代理与标记 table 映射并在那里填充它?

我浏览了这个文档:http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/associationproxy.html 我有点明白我需要 student_id 和标记之间的这个代理..这样我就可以查询 student_id.mark 来获得标记。但是我如何在我的 Junction table 中得到它?

有人可以指导我吗?

我建议它是一对多关系(一个学生 - 很多分数,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)

如果您想创建多对多关系,那么您可能需要考虑创建关联 table (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)

无论如何,您不需要 Junction 模型来创建学生和他们的分数之间的关系。