1. 外键字段在哪张表需要注意,因为外键在表中产生字段
  2. relationship段可在任意一张表,因为实际上不在表中产生字段

    一对多

    注意:一对多的外键必选在多表 ```python

    一对多 表关系

一个用户对应多篇文章

class User(Base): tablename = ‘user’

  1. id = Column(Integer, primary_key=True, autoincrement=True)
  2. username = Column(String(50), nullable=False)
  3. # 反向访问的属性
  4. articles = relationship("Article", backref='author') # User访问Article用 articles, 反向用 author
  5. def __str__(self):
  6. return "User(username:%s)" % self.username

class Article(Base): tablename = ‘article’

  1. id = Column(Integer, primary_key=True, autoincrement=True)
  2. title = Column(String(50))
  3. content = Column(Text, nullable=False)
  4. # 表中的字段
  5. uid = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'))
  6. # author = relationship("User") # 等效于User中 backref='author'
  7. def __str__(self):
  8. return "Article(Article:%s)" % self.title
  1. <a name="DRZYt"></a>
  2. ### CURD
  3. ```python
  4. # Base.metadata.drop_all()
  5. # Base.metadata.create_all()
  6. session = sessionmaker(bind=engine)()
  7. # 1.ORM 增加user、article表中的数据
  8. # user = User(username='ecithy')
  9. # session.add(user)
  10. # session.commit()
  11. # 查询
  12. # 根据文章查询作者
  13. article = session.query(Article).first()
  14. print(article.author)
  15. # 根据作者 查询文章
  16. user = session.query(User).first()
  17. article = user.articles
  18. print(article)
  19. for data in article:
  20. print(data)
  21. # print(user.articles)
  22. # 添加单条数据
  23. # user = session.query(User).first()
  24. # article = Article(title='python4', content='xxx')
  25. # article.author = user
  26. # session.add(article)
  27. # session.commit()
  28. # 添加多条数据
  29. # user = User(username='ecithy')
  30. # article1 = Article(title='python11', content='xxxx1')
  31. # article2 = Article(title='python22', content='xxxx2')
  32. #
  33. # user = session.query(User).get(2)
  34. # article1.author = user
  35. # article2.author = user
  36. # session.add(article1)
  37. # session.add(article2)
  38. # session.commit()

一对一

只需要在一对多的基础上,在外键字段加个 unique=True
注意:一对一的外键可在任意一张表,哪个表使用的少或者数据少,建议外键放在哪张表

  1. # 一对多 表关系
  2. # 一个用户对应多篇文章
  3. class User(Base):
  4. __tablename__ = 'user'
  5. id = Column(Integer, primary_key=True)
  6. username = Column(String(50), nullable=False)
  7. # 反向访问的属性
  8. articles = relationship("Article", backref='author') # User访问Article用 articles, 反向用 author
  9. def __str__(self):
  10. return "User(username:%s)" % self.username
  11. class Article(Base):
  12. __tablename__ = 'article'
  13. id = Column(Integer, primary_key=True)
  14. title = Column(String(50))
  15. content = Column(Text, nullable=False)
  16. # 表中的字段
  17. uid = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'), unique=True)
  18. # author = relationship("User") # 等效于User中 backref='author'
  19. def __str__(self):
  20. return "Article(Article:%s)" % self.title

多对多

注意:多对多的两个外键在第三张表

  1. # 中间表的定义
  2. teacher_classes = Table(
  3. "teacher_classes", # 表名
  4. Base.metadata, # 表结构信息
  5. Column('id', Integer, primary_key=True), # 主键
  6. Column('teacher_id', Integer, ForeignKey('teacher.id')), # 外键1
  7. Column('classes_id', Integer, ForeignKey('classes.id')) # 外键2
  8. )
  9. class Teacher(Base):
  10. __tablename__ = 'teacher'
  11. id = Column(Integer, primary_key=True)
  12. name = Column(String(50))
  13. classes = relationship('Classes', backref='teacher', secondary=teacher_classes)
  14. def __str__(self):
  15. return "Teacher(name:%s)" % self.name
  16. class Classes(Base):
  17. __tablename__ = 'classes'
  18. id = Column(Integer, primary_key=True)
  19. name = Column(String(50))
  20. def __str__(self):
  21. return "Classes(name:%s)" % self.name

CURD

  1. session = sessionmaker(bind=engine)()
  2. Base.metadata.drop_all()
  3. Base.metadata.create_all()
  4. teacher1 = Teacher(name='ecithy')
  5. teacher2 = Teacher(name='mf')
  6. classes1 = Classes(name='基础班')
  7. classes2 = Classes(name='进阶班')
  8. teacher1.classes.append(classes1)
  9. teacher1.classes.append(classes2)
  10. teacher2.classes.append(classes1)
  11. teacher2.classes.append(classes2)
  12. session.add(teacher1)
  13. session.add(teacher2)
  14. session.commit()
  15. # 老师对应的班级
  16. teacher = session.query(Teacher).first()
  17. print(teacher)
  18. for i in teacher.classes:
  19. print(i)
  20. # 班级对应的老师
  21. classes = session.query(Classes).first()
  22. for i in classes.teacher:
  23. print(i)