python - SQLAlchemy upsert -
python - SQLAlchemy upsert -
i create table 2 existence tables in different session.
for example:
user {uid, fname, lname} salary {uid, salary} => newtable {uid, full_name, salary}
first "add" user newtable, , salary null -> ok
n = newtable(uid=0, full_name=(user.fname+user.lname)) session.add(n) session.commit()
... commit, , else ...
and "merge" salary newtable, salary ok, full_name gone.
n2 = newtable(uid=0, salary=100) session.merge(n2) session.commit()
why? how can upsert field? or automatic combine this.
after all, found solution.
use "select , update" avoid overwriting whole new object
when inserting 2 tables
for first table:
session.add(doc)
for sec table:
session.query(newtable).filter(newtable.uid == doc.uid).update(doc)
python mysql sqlalchemy
Comments
Post a Comment