How to update SQLAlchemy row entry?
업데이트 하는 방법
1) user.no_of_logins += 1
session.commit()
2) session.query().\
filter(User.username == form.username.data).\
update({"no_of_logins": (User.no_of_logins +1)})
session.commit()
3) conn = engine.connect()
stmt = User.update().\
values(no_of_logins=(User.no_of_logins + 1)).\
where(User.username == form.username.data)
conn.execute(stmt)
4) setattr(user, 'no_of_logins', user.no_of_logins+1)
session.commit()
- flush를 사용할 경우
user.no_of_logins = User.no_of_logins + 1
session.flush()
user.no_of_logins = User.no_of_logins + 1
session.commit()
# result: UPDATE user SET no_of_logins = no_of_logins + 1 WHERE user.id = 6
flush랑 commit의 차이점 : https://hyeonukdev.tistory.com/183
반응형
'SW ENGINEERING > SqlAlchemy' 카테고리의 다른 글
[SQLAlchemy] flush vs commit (0) | 2022.03.08 |
---|---|
[SQLAlchemy] subquery (0) | 2022.03.08 |
최근댓글