[email protected] wrote:

> On 2015-03-08 11:17 Michael Bayer <[email protected]> wrote:
>> there’s no particular “SQLAlchemy way” to do this,
> 
> What is about
>       make_transient()
> ?
> 
> I don't understand this function 100%tly.

it changes the state of an object from persistent to transient. the states
are documented at
http://docs.sqlalchemy.org/en/rel_0_9/orm/session_state_management.html#quickie-intro-to-object-states.

I think the concept that might not be clear here is that the “foo.id” attribute 
on your object (assuming “id” is your primary key) and the “identity key” 
referred to in the documentation are not the same thing.   The “identity key” 
is the primary key of an object, *in the database*.     I can make an object:   
Foo(id=5), “5” is the value of the “id” attribute which corresponds to what 
would be the primary key column in the database, but there’s no “5” in the 
database.  No identity key.   I persist it, INSERT INTO table VALUES (5), now 
there’s a “5” in the database, now there’s an identity key.

this is a copy routine I recently implemented for openstack, perhaps it will
be helpful:

class Base(object):

    def __copy__(self):
        """Implement a safe copy.copy().

        SQLAlchemy-mapped objects travel with an object
        called an InstanceState, which is pegged to that object
        specifically and tracks everything about that object.  It's
        critical within all attribute operations, including gets
        and deferred loading.   This object definitely cannot be
        shared among two instances, and must be handled.

        The copy routine here makes use of session.merge() which
        already essentially implements a "copy" style of operation,
        which produces a new instance with a new InstanceState and copies
        all the data along mapped attributes without using any SQL.

        The mode we are using here has the caveat that the given object
        must be "clean", e.g. that it has no database-loaded state
        that has been updated and not flushed.   This is a good thing,
        as creating a copy of an object including non-flushed, pending
        database state is probably not a good idea; neither represents
        what the actual row looks like, and only one should be flushed.

        """
        session = orm.Session()

        copy = session.merge(self, load=False)
        session.expunge(copy)
        return copy




> 
> -- 
> 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.

-- 
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.

Reply via email to