一个用户对应多篇文章
class User(Base): tablename = ‘user’
id = Column(Integer, primary_key=True, autoincrement=True)
username = Column(String(50), nullable=False)
# 反向访问的属性
articles = relationship("Article", backref='author') # User访问Article用 articles, 反向用 author
def __str__(self):
return "User(username:%s)" % self.username
class Article(Base): tablename = ‘article’
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(50))
content = Column(Text, nullable=False)
# 表中的字段
uid = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'))
# author = relationship("User") # 等效于User中 backref='author'
def __str__(self):
return "Article(Article:%s)" % self.title
<a name="DRZYt"></a>
### CURD
```python
# Base.metadata.drop_all()
# Base.metadata.create_all()
session = sessionmaker(bind=engine)()
# 1.ORM 增加user、article表中的数据
# user = User(username='ecithy')
# session.add(user)
# session.commit()
# 查询
# 根据文章查询作者
article = session.query(Article).first()
print(article.author)
# 根据作者 查询文章
user = session.query(User).first()
article = user.articles
print(article)
for data in article:
print(data)
# print(user.articles)
# 添加单条数据
# user = session.query(User).first()
# article = Article(title='python4', content='xxx')
# article.author = user
# session.add(article)
# session.commit()
# 添加多条数据
# user = User(username='ecithy')
# article1 = Article(title='python11', content='xxxx1')
# article2 = Article(title='python22', content='xxxx2')
#
# user = session.query(User).get(2)
# article1.author = user
# article2.author = user
# session.add(article1)
# session.add(article2)
# session.commit()
一对一
只需要在一对多的基础上,在外键字段加个 unique=True
注意:一对一的外键可在任意一张表,哪个表使用的少或者数据少,建议外键放在哪张表
# 一对多 表关系
# 一个用户对应多篇文章
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
username = Column(String(50), nullable=False)
# 反向访问的属性
articles = relationship("Article", backref='author') # User访问Article用 articles, 反向用 author
def __str__(self):
return "User(username:%s)" % self.username
class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True)
title = Column(String(50))
content = Column(Text, nullable=False)
# 表中的字段
uid = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'), unique=True)
# author = relationship("User") # 等效于User中 backref='author'
def __str__(self):
return "Article(Article:%s)" % self.title
多对多
注意:多对多的两个外键在第三张表
# 中间表的定义
teacher_classes = Table(
"teacher_classes", # 表名
Base.metadata, # 表结构信息
Column('id', Integer, primary_key=True), # 主键
Column('teacher_id', Integer, ForeignKey('teacher.id')), # 外键1
Column('classes_id', Integer, ForeignKey('classes.id')) # 外键2
)
class Teacher(Base):
__tablename__ = 'teacher'
id = Column(Integer, primary_key=True)
name = Column(String(50))
classes = relationship('Classes', backref='teacher', secondary=teacher_classes)
def __str__(self):
return "Teacher(name:%s)" % self.name
class Classes(Base):
__tablename__ = 'classes'
id = Column(Integer, primary_key=True)
name = Column(String(50))
def __str__(self):
return "Classes(name:%s)" % self.name
CURD
session = sessionmaker(bind=engine)()
Base.metadata.drop_all()
Base.metadata.create_all()
teacher1 = Teacher(name='ecithy')
teacher2 = Teacher(name='mf')
classes1 = Classes(name='基础班')
classes2 = Classes(name='进阶班')
teacher1.classes.append(classes1)
teacher1.classes.append(classes2)
teacher2.classes.append(classes1)
teacher2.classes.append(classes2)
session.add(teacher1)
session.add(teacher2)
session.commit()
# 老师对应的班级
teacher = session.query(Teacher).first()
print(teacher)
for i in teacher.classes:
print(i)
# 班级对应的老师
classes = session.query(Classes).first()
for i in classes.teacher:
print(i)