flask项目之SQLAlchemy操作(4)

34 篇文章 1 订阅
10 篇文章 0 订阅


1 新增

1.1 单个添加
user = User(mobile='15612345678', name='itcast')
db.session.add(user)
db.session.commit()
profile = Profile(id=user.id)
db.session.add(profile)
db.session.commit()
1.2 批量添加
db.session.add_all([user1, user2, user3])
db.session.commit()

2 删除

2.1 方式一
  user = User.query.order_by(User.id.desc()).first()
  db.session.delete(user)
  db.session.commit()
2.1 方式二
  User.query.filter(User.mobile='18512345678').delete()
  db.session.commit()

3 更新

3.1 方式一
  user = User.query.get(1)
  user.name = 'Python'
  db.session.add(user)
  db.session.commit()
3.2 方式二
  User.query.filter_by(id=1).update({'name':'python'})
  db.session.commit()

4 查询

4.1 all()

查询所有,返回列表

User.query.all()
4.2 first()

查询第一个,返回对象

User.query.first()
4.3 get()

根据主键ID获取对象,若主键不存在返回None

User.query.get(2)

另一种查询方式

db.session.query(User).all()
db.session.query(User).first()
db.session.query(User).get(2)
4.4 filter_by

进行过虑

User.query.filter_by(mobile='13911111111').first()
User.query.filter_by(mobile='13911111111', id=1).first()  # and关系
4.5 filter

进行过虑

User.query.filter(User.mobile=='13911111111').first()

逻辑或

from sqlalchemy import or_
User.query.filter(or_(User.mobile=='13911111111', User.name.endswith('号'))).all()

逻辑与

from sqlalchemy import and_
User.query.filter(and_(User.name != '13911111111', User.mobile.startswith('185'))).all()

逻辑非

from sqlalchemy import not_
User.query.filter(not_(User.mobile == '13911111111')).all()
4.6 offset

偏移,起始位置

User.query.offset(2).all()
4.7 limit

获取限制数据

User.query.limit(3).all()
4.8 order_by

排序

User.query.order_by(User.id).all()  # 正序
User.query.order_by(User.id.desc()).all()  # 倒序
4.9 复合查询
User.query.filter(User.name.startswith('13')).order_by(User.id.desc()).offset(2).limit(5).all()
query = User.query.filter(User.name.startswith('13'))
query = query.order_by(User.id.desc())
query = query.offset(2).limit(5)
ret = query.all()
4.10 优化查询
user = User.query.filter_by(id=1).first()  # 查询所有字段
select user_id, mobile......

select * from   # 程序不要使用
select user_id, mobile,.... # 查询指定字段

from sqlalchemy.orm import load_only
User.query.options(load_only(User.name, User.mobile)).filter_by(id=1).first() # 查询特定字段
4.11 聚合查询
from sqlalchemy import func

db.session.query(Relation.user_id, func.count(Relation.target_user_id)).filter(Relation.relation == Relation.RELATION.FOLLOW).group_by(Relation.user_id).all()
4.12 关联查询
  1. 使用ForeignKey
class User(db.Model):
    ...
    profile = db.relationship('UserProfile', uselist=False)
    followings = db.relationship('Relation')

class UserProfile(db.Model):
    id = db.Column('user_id', db.Integer, db.ForeignKey('user_basic.user_id'), primary_key=True,  doc='用户ID')
    ...

class Relation(db.Model):
    user_id = db.Column(db.Integer, db.ForeignKey('user_basic.user_id'), doc='用户ID')
    ...

# 测试   
user = User.query.get(1)
user.profile.gender
user.followings
  1. 使用primaryjoin
class User(db.Model):
    ...

    profile = db.relationship('UserProfile', primaryjoin='User.id==foreign(UserProfile.id)', uselist=False)
    followings = db.relationship('Relation', primaryjoin='User.id==foreign(Relation.user_id)')

# 测试
user = User.query.get(1)
user.profile.gender
user.followings
  1. 指定字段关联查询
class Relation(db.Model):
    ...
    target_user = db.relationship('User', primaryjoin='Relation.target_user_id==foreign(User.id)', uselist=False)

from sqlalchemy.orm import load_only, contains_eager

Relation.query.join(Relation.target_user).options(load_only(Relation.target_user_id), contains_eager(Relation.target_user).load
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值