SQLAlchemy关系示例

本文详细介绍了ORM(对象关系映射)在Python中的使用,包括基础数据库模型、一对一关系、一对多关系、多对多关系的建立,以及关联对象的处理。通过示例展示了如何定义外键、反向关系和中间表,以实现不同类型的关联操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基础表

class Base(db.Model):
    """基础数据库模型:提供id、创建时间、更新时间"""
    __abstract__ = True
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}  # 支持中文
    id = Column(Integer, primary_key=True, comment='主键')
    create_time = Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')

一对一

class Parent(Base):
    __tablename__ = 'parent'
    child = relationship("Child", uselist=False, back_populates="parent")

class Child(Base):
    __tablename__ = 'child'
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent", back_populates="child")

一对多

在一对多中建立双向关系,“反向”端是多对一,指定一个附加的 relationship() 然后用 relationship.back_populates 参数:

class Parent(Base):
    __tablename__ = 'parent'
    children = relationship("Child", back_populates="parent")

class Child(Base):
    __tablename__ = 'child'
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent", back_populates="children")

多对多

class_student = Table(
	'class_student ', 
    Column('class_id', Integer, ForeignKey('class.id')),
    Column('student_id', Integer, ForeignKey('student.id'))
)

class Class(Base):
    __tablename__ = 'class'
    id = Column(Integer, primary_key=True)
    students = relationship("Student", secondary=class_student, back_populates="classes")

class Student(Base):
    __tablename__ = 'student'
    id = Column(Integer, primary_key=True)
    classes= relationship("Class", secondary=class_student, back_populates="students")

关联对象

class ClassStudent(Base):
    __tablename__ = 'class_student'
    class_id = Column(Integer, ForeignKey('class.id'))
    student_id = Column(Integer, ForeignKey('student.id'))
    extra_data = Column(String(50))
    student = relationship("Student", back_populates="classes")
    _class = relationship("Class", back_populates="students")

class Class(Base):
    __tablename__ = 'class'
    students = relationship("ClassStudent", back_populates="_class")

class Student(Base):
    __tablename__ = 'student'
    classes= relationship("ClassStudent", back_populates="student")

多个外键指向同一个表


class Book(Base):
    __tablename__ = 'book'
    
    class_book1 = relationship("Class",foreign_keys="Class.book1_id", back_populates="book1")
    class_book2 = relationship("Class",foreign_keys="Class.book2_id", back_populates="book2")

class Class(Base):
    __tablename__ = 'class'
    
    book1_id = db.Column(db.Integer, db.ForeignKey(Book.id), comment='一类图书')
    book1= relationship(Book, foreign_keys=book1_id, back_populates="class_book1")

	book2_id = db.Column(db.Integer, db.ForeignKey(Book.id), comment='二类图书')
    book2= relationship(Book, foreign_keys=book2_id, back_populates="class_book2")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

llc的足迹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值