[sqlalchemy] Re: Is there a way to replace object in DB?

2008-01-16 Thread Denis S. Otkidach
On Jan 15, 2008 6:54 PM, Michael Bayer [EMAIL PROTECTED] wrote: The last commit fails with: sqlalchemy.exceptions.IntegrityError: (IntegrityError) Referers.objectId may not be NULL u'UPDATE Referers SET objectId=? WHERE Referers.id = ?' [None, 1] right thats because the instance doesnt

[sqlalchemy] Re: Is there a way to replace object in DB?

2008-01-16 Thread Michael Bayer
On Jan 16, 2008, at 4:43 AM, Denis S. Otkidach wrote: On Jan 15, 2008 6:54 PM, Michael Bayer [EMAIL PROTECTED] wrote: The last commit fails with: sqlalchemy.exceptions.IntegrityError: (IntegrityError) Referers.objectId may not be NULL u'UPDATE Referers SET objectId=? WHERE

[sqlalchemy] Re: Is there a way to replace object in DB?

2008-01-15 Thread Denis S. Otkidach
On Jan 11, 2008 8:41 PM, Michael Bayer [EMAIL PROTECTED] wrote: what that looks like to me is that you're attempting to query the database for object ID #1 using merge(). when you merge(), its going to treat the object similarly to how it does using session.save_or_update(). that is, it

[sqlalchemy] Re: Is there a way to replace object in DB?

2008-01-15 Thread Michael Bayer
On Jan 15, 2008, at 8:20 AM, Denis S. Otkidach wrote: On Jan 11, 2008 8:41 PM, Michael Bayer [EMAIL PROTECTED] wrote: what that looks like to me is that you're attempting to query the database for object ID #1 using merge(). when you merge(), its going to treat the object similarly to

[sqlalchemy] Re: Is there a way to replace object in DB?

2008-01-11 Thread Michael Bayer
On Jan 11, 2008, at 12:30 PM, Denis S. Otkidach wrote: # 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() what that looks like to me is that you're attempting to

[sqlalchemy] Re: Is there a way to replace object in DB?

2008-01-11 Thread Denis S. Otkidach
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

[sqlalchemy] Re: Is there a way to replace object in DB?

2007-12-28 Thread Michael Bayer
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

[sqlalchemy] Re: Is there a way to replace object in DB?

2007-12-28 Thread Denis S. Otkidach
On Dec 28, 2007 1:00 AM, Rick Morrison [EMAIL PROTECTED] wrote: Here's the idiom that should work: def ensure_object(sess, id): o = sess.Query(ModelObject).get(id)# if found, o is now loaded into session if not o: o = ModelObject(1, u'title') sess.save(o)

[sqlalchemy] Re: Is there a way to replace object in DB?

2007-12-27 Thread Rick Morrison
I think you're thinking of .load() .get() does not throw on not found. On 12/27/07, braydon fuller [EMAIL PROTECTED] wrote: you can also use 'try' to avoid error messages: def ensure_object(db, id): try: o = db.Query(ModelObject).get(id) except: o =

[sqlalchemy] Re: Is there a way to replace object in DB?

2007-12-27 Thread Denis S. Otkidach
On Dec 26, 2007 10:38 PM, Michael Bayer [EMAIL PROTECTED] wrote: if you have an instance which you are unsure if it already exists, you can add it to a session using session.save_or_update(instance). The decision between INSERT and UPDATE is ultimately decided by the presence of an attribute

[sqlalchemy] Re: Is there a way to replace object in DB?

2007-12-27 Thread Rick Morrison
...if you're just checking to see if something exists in the database, why not just try to .load() it, and then construct it afresh if you don't find it? This kind of operation is sometimes called an upsert ...some database engines support it, some don't. Most don't. But what all database engines

[sqlalchemy] Re: Is there a way to replace object in DB?

2007-12-27 Thread braydon fuller
you can also use 'try' to avoid error messages: def ensure_object(db, id): try: o = db.Query(ModelObject).get(id) except: o = ModelObject(1, u'title') db.save(o) db.commit() return o -Braydon Rick Morrison wrote: ...if you're just checking to see