1

I have an object

esc = Escalation.query\
    .filter(Escalation.esc_id == esc_id).first()

And I would like to copy all of the columns except the primary key and foreign keys so that I can make a new copy of this object in the database with new relationships.

Currently I just make a constructor that can take a Escalation instance so I can say

new_esc = Escalation(esc)

but I was wondering if there is a built in feature for SQLAlchemy that solves this problem. I looked at session.merge but I don't want to take the entire state.

AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103
  • 1
    Take a look at [`make_transient`](http://docs.sqlalchemy.org/en/latest/orm/session.html#sqlalchemy.orm.session.make_transient), and also see similar question http://stackoverflow.com/questions/14636192/sqlalchemy-modification-of-detached-object. You still might deal with the foreign keys though. – van Nov 05 '14 at 06:06

1 Answers1

1

I think make_transient looks like a great solution. This is not built-in, but I've subclassed sqlalchemy.ext.declarative.DeclarativeMeta to have a custom __init__ and gave it some iteration methods, so that you could unpack instance values into a new instance like this:

new_esc = Escalation(**esc)

It was kind of a pain to do, though. Metaclasses are fragile and hurt my brain.

jkmacc
  • 6,125
  • 3
  • 30
  • 27