Hi Michael,
With some more detail:
What i do is:
1 make a new object (mapped)-> obj1
2 create a scoped session (with context manager)-> session1
3 do session1.add(obj)
4 create another scoped session -> session2
5 do session2.query(someclass).get(some_id)->obj2
6 close session2, no commit, no flush -> obj2 is detached (right?)
7 do obj1.someattr.append(obj2)
8 do session1.commit()
9 get the first ERROR above
basically i use
def Session( objs):
session = session_maker()
for obj in objs:
if object_session(obj) is None:
session.add(obj)
else:
session.merge(obj)
return session
@contextmanager
def scoped_session(objs = [], commit = True):
session = Session(objs)
try:
yield session
if commit:
session.commit()
except:
session.rollback()
raise
finally:
session.close()
and essentially code description (1-8) above comes down to:
obj1 = cls1()
with scoped_session([obj1]) as session1:
obj1.somefield = "somevalue"
with scoped_session(commit = False) as session2:
obj2 = session2.query(cls2).get(some_id)
obj1.someattr.append(obj2)
if i just do:
with scoped_session([obj1]) as session1:
obj1.somefield = "somevalue"
there is no problem.
Also:
"this means an object was meant to be UPDATEed via the ORM, however the row
which is the target of the UPDATE is missing. Either the primary key of
this row changed somehow, or the row was deleted, *or* the row is not
visible to your transaction (this seems to be your case)."
- could the error also occur when the object was never committed to the
database (which seems to be the case; the commit where the error occurs
should be the first time the Company object is committed to the database)?
- this seems to suggest that it is possible that a row is in the database,
but that it is not visible to a transaction; is that possible?
As far as i know in the code that causes the problem, i do not do any
deletes and i do not call flush myself.
Doing some more testing, now i get more of the second error in:
def __str__(self): #in mapped class
print object_session(self) is not None, has_identity(self) # True,
True, <= OK
print self.id #<= ERROR
......
with trace:
File "d:\Documents\Code\python\floware\models\flow\processes.py", line 333,
in run
self.execute(input, output)
File "d:\Documents\Code\python\floware\toolshed\logs.py", line 55, in
wrapper
f_result = func(*v, **k)
File "d:\Documents\Code\python\floware\models\flow\libraries\basic.py",
line 159, in execute
print "%s %s" % (self.cursor, str(i.item))
File "d:\Documents\Code\python\floware\models\data\database.py", line 281,
in __str__
print object_session(self), has_identity(self), self.id
File
"C:\python27\lib\site-packages\sqlalchemy-0.8.3-py2.7-win32.egg\sqlalchemy\orm\attributes.py",
line 316, in __get__
File
"C:\python27\lib\site-packages\sqlalchemy-0.8.3-py2.7-win32.egg\sqlalchemy\orm\attributes.py",
line 611, in get
File
"C:\python27\lib\site-packages\sqlalchemy-0.8.3-py2.7-win32.egg\sqlalchemy\orm\state.py",
line 380, in __call__
File
"C:\python27\lib\site-packages\sqlalchemy-0.8.3-py2.7-win32.egg\sqlalchemy\orm\loading.py",
line 606, in load_scalar_attributes
sqlalchemy.orm.exc.ObjectDeletedError: Instance '<Company at 0x5e4a3f0>'
has been deleted, or its row is otherwise not present.
CL
>
>
--
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/groups/opt_out.