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 "Referers".id = ?' [None, 1]
>>
>> right thats because the instance doesnt exist yet. its better for
>> you
>> to just use the straight ahead query.get(), if None then save()
>> approach.
>
> Do you mean obj2 (id of which should go to objectId column in this
> UPDATE)? If so, why it doesn't exist? It's saved (updated in fact)
> just several lines above, so it must be both in DB and in session.
>
> The last lines of the original test case for convenience:
> [...]
> obj2 = replace(session, ModelObject(1, u'title2'))
> session.commit()
>
> ref2 = ModelReferer(1, obj2)
> replace(session, ref2)
> session.commit()
I cant tell what your issue is there without the backing data showing
the full picture. We dont support manipulating "_instance_key"
manually as a supported use case, so issues are not surprising. heres
an example using public APIs:
def replace(session, cls, id, **kwargs):
obj = session.query(cls).get(id)
if obj is None:
obj = cls(id=id, **kwargs)
session.save(obj)
else:
for key in kwargs:
setattr(obj, key, kwargs[key])
return obj
obj2 = replace(session, ModelObject, 1, title=u'title2')
session.commit()
ref2 = replace(session, ModelReferer, 1, object=obj2)
session.commit()
the above will also use less SQL than how you were doing it. if that
function is producing the same issue, provide a test case that
includes the supporting data and runs fully since from your example I
dont know if ModelReferer(1) is supposed to be present or not.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---