我在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_id 和标记外键(标记)填充联结表在每个主题中获得的内容被插入到标记表中)。我如何完成这项工作,我的意思是我可以在学生ID表中创建一个关联代理,该表与Mark表一起映射并在那里填充?
我浏览了这个文档:http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/associationproxy.html我有点明白我需要在student_id和marks之间使用这个代理。所以我可以查询student_id.mark来获取标记。但是我如何在我的Junction表中得到它?
有人可以指导我吗?
答案 0 :(得分:1)
我建议这是一对多关系(一个学生 - 很多分数,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)
无论如何,你不需要Junction模型来创建学生和他们的商标之间的关系。