What's "DBSession" ?
If it isn't the session useg by TG than you must explicit the commit()
Gla
This looks like the problems I had a month or two ago:
http://www.nabble.com/Bug-in-zope.sqlalchemy-td22685504.html
It eventually led to me removing zope.transaction-handling from our
application stack, as even with the described solution it didn't solve
the problem for us.
Others have claimed that things work for them - dunno about that.
Diez
It works if you use the session for bulk deletes or updates. But it does
not work if you do inserts that way past the session.
-- Christoph
The problem here is that you're actually not using the db session here,
but doing things past the db session.
The right and much simpler way is to add your data records to the
session using DBSession.add().
In this case, your table looks like an association table between a User
and Org class. Usually the User class then has an orgs attribute which
is a list, and vice versa (see how it is implemented in the auth model).
You would then simply append the org to the user.orgs list.
-- Christoph
What does "past the session" mean here? Or better yet, how does one do the
bulk-*-stuff?
Diez
What happens if you leave out the connection()-call? Directly using execute on
the session should be supported (and thus use the session)
Diez
What I meant is that you're executing an sql statement bypassing the
session. The session is not affected by that.
> Or better yet, how does one do the bulk-*-stuff?
For instance, you can do a bulk delete like this:
DBSession.query(User).filter(User.is_spammer == True).delete()
But that works only with deletes and updates, not with inserts.
-- Christoph
This is only a shortcut and should not make any difference. It works,
but the problem is that zope.sqlalchemy does not notice that data was
modified and therefore does not commit the transaction.
It should works as soon as you add any other statement that marks the
session as dirty, like this dummy "bulk" delete:
@expose()
def insert_a_record(self):
DBSession.execute(foo_table.insert({'user_id':2, 'org_id': 2}))
DBSession.query(User).filter(User.user_id == None).delete()
return "Successful!"
-- Christoph
That works as well, you can append to that object as if it was a list
(since it's not a normal Query object but an "AppenderQuery" object).
-- Christoph
Should not be necessary - it works for me without any dirty hacks.
In a quickstarted project I changed Permission.groups to
groups = dynamic_loader(Group, secondary=group_permission_table)
and then the following just worked:
@expose()
def insert_a_record(self):
p = DBSession.query(Permission).get(1)
g = DBSession.query(Group).get(1)
p.groups.append(g)
return "Successful!"
Tested with SA 0.5.4 and zope.sa 0.4.
-- Christoph