2

I would like to create a table from two existence tables in different session.

For example:

User {uid, fname, lname}
Salary {uid, salary}
=> NewTable {uid, full_name, salary}

First I "add" User into NewTable, and salary is null -> OK

n = NewTable(uid=0, full_name=(user.fname+user.lname))
session.add(n)
session.commit()

... commit, and do something else ...

And then I "merge" Salary into NewTable, salary is OK, BUT full_name is gone.

n2 = NewTable(uid=0, salary=100)
session.merge(n2)
session.commit()

Why? How can I upsert just some field? Or just automatic combine like this.

jackypan1989
  • 2,786
  • 1
  • 13
  • 11

1 Answers1

0

After all, I found my solution.

Use "select and update" to avoid overwriting a whole new object

when inserting from two tables


for first table:

session.add(doc)

for second table:

session.query(NewTable).filter(NewTable.uid == doc.uid).update(doc)
jackypan1989
  • 2,786
  • 1
  • 13
  • 11