sqlalchemy 更新column,如果存在则更新,如果不存在,则添加新记录
class Friendship(Base):
__tablename__ = 'friendship'
id = Column(Integer,primary_key=True)
me = Column(Integer,ForeignKey('users.id'))
friend = Column(Integer,ForeignKey('users.id'))
def __init__(self,me,friend):
self.me = me
self.friend = friend
def check_existing(self):
existing = session.query(Friendship).filter_by(me=self.me,friend=self.friend).first()
if not existing:
friendship = Friendship(self.me,self.friend)
else:
friendship = existing
session.close()
return friendship
if __name__ == '__main__':
friendship = Friendship(self.from_user_id,self.friend_id)
friendship = friendship.check_existing()
session.add(friendship)
session.commit()
session.close()
原文地址:https://www.cnblogs.com/We612/p/12122300.html
时间: 2024-10-10 08:16:55