Is it possible to move a pending object that is a child in a relation to 
a new sessions without flushing the objects original session?

---------------------------------

import sqlalchemy
from sqlalchemy import *
from sqlalchemy.orm import *

uri = 'sqlite:///:memory:'
metadata = MetaData()

genus_table = Table('genus', metadata,
                     Column('id', Integer, primary_key=True),
                     Column('genus', String(32), nullable=False))

species_table = Table('species', metadata,
                       Column('id', Integer, primary_key=True),
                       Column('sp', String(64)),
                       Column('genus_id', Integer, ForeignKey('genus.id'),
                              nullable=False))

class Genus(object):
     pass

class Species(object):
     pass

mapper(Genus, genus_table,
     properties = {
     'species': relation(Species, cascade='all, delete-orphan',
                         backref=backref('genus', uselist=False))})
mapper(Species, species_table)


engine = create_engine(uri)
engine.connect()
metadata.bind = engine
metadata.create_all()

print 'SQLAlchemy: %s' % sqlalchemy.__version__
try:
     sqlalchemy.version
     sm = sessionmaker(bind=engine, autoflush=False, transactional=False)
     session = sm()
     new_session = sm()
except:
     session = create_session()
     new_session = create_session()


genus_table.insert().execute({'id': 1, 'genus': 'Somegenus'})

g = session.load(Genus, 1)
if True:
     s = Species()
     s.genus = g
else:
     s = Species()
     g.species.append(s)

if False:
     s2 = Species()
     session.expunge(s)
     for prop in object_mapper(s).iterate_properties:
         name = prop.key
         value = getattr(s, name)
         if value not in (None, []) and hasattr(value, '_instance_key'):
             value = new_session.load(value.__class__, value.id)
         setattr(s2, name, value)
     new_session.save_or_update(s2)
else:
     s2 = new_session.merge(s)
     session.expunge(s)

new_session.flush()
#session.flush() # uncomment this to make it work

# should print [1]
print [s.genus_id for s in g.species]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to