Also, this might help to clarify my goal, this is my current working code that accomplishes the desired result, but with more introspection than I was hoping for. If there isn't something easier, this will work.
# instance1 is an instance of Class1, i want to create Class2 with the same pk keys = [key.name for key in class_mapper(Class2).primary_key] cls, values = identity_key(instance=instance1) kwargs = dict(zip(keys, values)) instance2 = Class2(**kwargs) I'm basically looking for a shortcut for that, if it exists. JT On Wednesday, August 27, 2014 6:17:55 PM UTC-7, Gerald Thibault wrote: > > > > On Wednesday, August 27, 2014 4:58:50 PM UTC-7, Michael Bayer wrote: >> >> >> On Aug 27, 2014, at 6:17 PM, Gerald Thibault <[email protected]> >> wrote: >> >> > I have a pretty simple goal, so simple I probably don't even need >> example code. >> > >> > I have 2 classes, and I want to pull the identity key from an instance >> of one class, and use the values to create an instance of the second, >> without requiring (or introspecting) things like the PK names, FK names >> linking them, etc. Something like: >> > >> > instance1 = Class1() >> > cls, vals = identity_key(instance=instance1) >> > instance2 = Class2.from_identity_key(vals) # that method doesn't exist >> > >> > I am trying to do this without requiring any of the PK column names or >> FK info linking the 2 tables together (they are a 1-to-1 relationship). >> given only an instance of Class1 and the class "Class2", I want to create >> the instance of Class2. >> > >> > Is this possible? Is there a way to "reverse" identity_key, so instead >> of instance -> (cls, vals) I could do (cls, vals) -> instance? >> >> OK well how to the primary key attributes of Class1 and Class2 relate? >> If Class1’s primary key columns are “a”, “b”, and Class2’s primary key >> columns are, “id”, how is that mapping done? are we assuming same number >> of columns in the PK of both, in the same order, or same names, or what? >> >> > The pks are named differently in every case, which is why i was looking > for a way that didn't involve use of keys at all. For every case, the > length of the pks are the same, and the columns comprising the key are in > the same order. > > The mapping is done by a standard relationship, but I was thinking I > wouldn't need the mapping at all if i am just pulling and pushing a pk > value. The name of the relationship is different in every case. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
