[关闭]
@gzm1997 2018-05-16T15:59:16.000000Z 字数 2744 阅读 1414

sqlalchemy级联

mysql sqlalchemy


relationship

relationship是sqlalchemy中的用于创建两个对象之间的关系的一个对象,跟原本的mysql没有太大关系,但是relationship一定要建立在具有外键关系的两个对象上

例如

  1. class User(Base):
  2. __tablename__ = "user"
  3. id = Column(Integer, primary_key = True)
  4. name = Column(String(20))
  5. class Article(Base):
  6. __tablename__ = "article"
  7. id = Column(Integer, primary_key = True)
  8. user_id = Column(Integer, ForeignKey("user.id"))
  9. user = relationship("User", order_by = "Article.id", backref = backref("article"), cascade = "save-update, merge, delete")

user和article之间是一对多的关系,由于article具有

user = relationship("User", order_by = "Article.id", backref = backref("article"), cascade = "save-update, merge, delete")

字段,所以article具有user属性,也即article.user可以获取article对应的user

同时因为

backref = backref("article")

使得user具有article属性,由外键关系知道user和article为一对多关系,所以,user.article类型为[]。

backref和back_populates
stackoverflow上对两者的解释

If you use backref you don't need to declare the relationship on the second table

就是使用backref就不用再两个具有外键的类上都声明relationship。

backref

  1. class Parent(Base):
  2. __tablename__ = 'parent'
  3. id = Column(Integer, primary_key=True)
  4. children = relationship("Child", backref="parent")
  5. class Child(Base):
  6. __tablename__ = 'child'
  7. id = Column(Integer, primary_key=True)
  8. parent_id = Column(Integer, ForeignKey('parent.id'))

back_populates

  1. class Parent(Base):
  2. __tablename__ = 'parent'
  3. id = Column(Integer, primary_key=True)
  4. children = relationship("Child", back_populates="parent")
  5. class Child(Base):
  6. __tablename__ = 'child'
  7. id = Column(Integer, primary_key=True)
  8. parent_id = Column(Integer, ForeignKey('parent.id'))
  9. parent = relationship("Parent", back_populates="children")

上面两者是等价的


cascade级联

例子中

cascade = "save-update, merge, delete"

属性用来表示这两个关联起来的对象的增删的时候之间的关系

cascade默认值为save-update, merge

为了表达更清晰,声明了relationship属性的类我们称之为子类,没有声明的我们称之为父类,使用上面的user和article例子
image_1cdi1seaqu2sntue61gq51la29.png-6.8kB

1.save-update

2.delete

3.delete-orphan

delete-orphan实在delete的基础上,加上当子对象被解除跟父对象的关系的时候就会被标志为删除

4.其他还有merge,refresh-expire,expunge那些不常用的就不要求理解了


测试代码

  1. from sqlalchemy import Column, String, Integer, ForeignKey, create_engine
  2. from sqlalchemy.orm import sessionmaker, relationship, backref
  3. from sqlalchemy.ext.declarative import declarative_base
  4. Base = declarative_base()
  5. class User(Base):
  6. __tablename__ = "user"
  7. id = Column(Integer, primary_key = True)
  8. name = Column(String(20))
  9. class Article(Base):
  10. __tablename__ = "article"
  11. id = Column(Integer, primary_key = True)
  12. user_id = Column(Integer, ForeignKey("user.id"))
  13. user = relationship("User", order_by = "Article.id", backref = backref("article"), cascade = "save-update, merge, delete")
  14. engine = create_engine('mysql+mysqlconnector://root:Gzm20125@localhost:3306/test')
  15. DBSession = sessionmaker(bind = engine)
  16. Base.metadata.drop_all(engine)
  17. Base.metadata.create_all(engine)
  18. # 创建session对象:
  19. session = DBSession()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注