flask_sqlaachemy

外键:
from sqlalchemy import ForeignKey
uid = Column(Integer, ForeignKey(‘user.id‘, ondelete=‘SET NULL‘))
ondelete 级联删除数据

表关系:
from sqlalchemy.orm import relationship
author = relationship(‘User‘)
过滤条件:
eq: filter(User.id!=‘3‘).all()
not eq: filter(~User.id==‘3‘).all()
and_: filter(Artical2.title == ‘title0‘, Artical2.price == 5)
filter(and_(Artical2.title == ‘title1‘, Artical2.price == 10)).all()
filter(Artical2.title == ‘title1‘).filter(Artical2.price == 10).all()
filter(Artical2.title == ‘title1‘, Artical2.price == 10).all()
like: filter(Artical2.title.like(‘%title%‘)).all()
# in 关键字      __xx 表示私有的方法
# [‘title2‘,‘title4‘]
# res = session.query(Artical2).filter(Artical2.title.in_([‘title2‘,‘title4‘])).all()
# not in
# res = session.query(Artical2).filter(Artical2.title.notin_([‘title2‘,‘title4‘])).all()
# res = session.query(Artical2).filter(~Artical2.title.in_([‘title2‘,‘title4‘])).all()
# null != 空
# res = session.query(Artical2).filter(~Artical2.title.in_([‘title2‘,‘title4‘])).all()

# or
res = session.query(Artical2).filter(or_(Artical2.title == ‘title1‘, Artical2.price == 43)).all()
# 外键的查询
# artical = session.query(Artical).first()
# uid = artical.uid
# print(uid)
# user = session.query(User).get(uid)
# print(user)
# 通过外键关联查询
user1 = session.query(User).filter(User.id == Artical.uid).first()

# 引擎: innodb:支持外键  myisam: 不支持外键
# 外键约束保证数据的有效性
# ondelete=‘CASCADE‘级联删除
# ondelete=‘SET NULL‘ 只删除子表





flask_sqlaachemy

上一篇:SQL-select中使用条件判断语句


下一篇:ssh-copy-id