On Dec 28, 2007 6:25 PM, Michael Bayer <[EMAIL PROTECTED]> wrote:
> On Dec 28, 2007, at 5:50 AM, Denis S. Otkidach wrote:
> > Sure, I can get an object from DB and copy data from new one. But
> > there is a lot of object types, so have to invent yet another meta
> > description for it (while it already exists in sqlalchemy). And
> > requirements changes often, so I have to change scheme in 2 places.
> > This is not good and error prone. Why I have to invent new description
> > when there is already one from sqlalchemy mapping? Can't I use it for
> > my purpose? Something like merge(objFromDB, newObj) will solve the
> > problem.
>
> session.merge() does copy the attributes of one object into another.
> theres some bugs with dont_load that have been fixed in trunk so try
> out the trunk if you have problems.
This doesn't work: I have the same IntegrityError or FlushError
depending on whether original object exists in the session (line
session.clear() is commented in the code below). What I do wrong?
--->8---
import sqlalchemy as sa, logging
from sqlalchemy.orm import mapper, sessionmaker
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.basicConfig()
class ModelObject(object):
def __init__(self, id, title):
self.id = id
self.title = title
metadata = sa.MetaData()
objectTable = sa.Table(
'Objects', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('title', sa.String(255), nullable=False),
)
objectsMapper = mapper(ModelObject, objectTable)
engine = sa.create_engine('sqlite://')
metadata.create_all(engine, checkfirst=True)
session = sessionmaker(bind=engine)()
obj1 = ModelObject(1, u'title1')
session.save(obj1)
session.commit()
session.clear()
# Another program. We have to insure that object with id=1 exists in DB and has
# certain properties.
obj2 = ModelObject(1, u'title2')
session.merge(obj2)
session.commit()
--->8---
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---