InvalidRequestError from CrudRestController post()/put() or Sprox create()/update()

29 views
Skip to first unread message

Moritz Schlarb

unread,
Nov 12, 2012, 12:31:38 PM11/12/12
to TurboGears, Sprox
Hi there you all.

I seem to have a problem regarding tgext.crud and Sprox when creating or
updating objects:

If, due to, e.g. an unique Index over multiple columns or other advanced
stuff, the flush in the Sprox SAORMProvider create() and update()
methods throws an IntegrityError, the catch_errors decorator on
CrudRestController catches that exception and flashes its message.

So far, so good, BUT, and this is were I'm not sure if it's a bug or if
I'm just doing something very stupid without knowing: I get an
InvalidRequestError (see traceback attached) while rendering the
previous form page again.
Apparently, this is because I use objects (e.g. an instance of Event in
that case) from the session to dynamically generate a menu on the page.
These objects aren't accessible due to the transactions failed state.
If I follow the instructions from the traceback Exception and wrap the
code for provider.create with try..except and issue a
provider.session.rollback(), I can get it to work.

Sadly, I can't hook the post and put methods because before_render never
gets called due to the exception...

Now the final question is:
- Am I doing anything stupid with my session-bound objects?
- Should CrudRestController or even Sprox directly perform the rollback
when the session.flush() fails?

Hoping for some insight ;)

Best wishes,

Moritz

--
Moritz Schlarb
traceback.log
Message has been deleted

Moritz Schlarb

unread,
Nov 12, 2012, 1:05:55 PM11/12/12
to TurboGears, Sprox
I was trying to hook in the wrong place!
Of course I need to do the rollback before the error_handler function is
called, which means before_call of new and edit!

It seems to work as done in
https://github.com/moschlar/SAUCE/commit/a7a2867cac8a255cbc6e804e33f922be3736b589

Still up for the discussion if it's a bug or a feature! ;)

Alessandro Molina

unread,
Nov 12, 2012, 6:56:54 PM11/12/12
to sprox
The latest tgext.crud 0.5.5 release should make possible to access the session to query objects after the validation failed, also provides fixes the pagination issue with pycopg.


--
You received this message because you are subscribed to the Google Groups "sprox" group.
To post to this group, send email to sp...@googlegroups.com.
To unsubscribe from this group, send email to sprox+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sprox?hl=en.


Moritz Schlarb

unread,
Nov 13, 2012, 6:00:41 AM11/13/12
to sp...@googlegroups.com
Mh, amol!

I'm a little bit hesitating to use SQLAlchemy events, because that would require lots of changes, since I effectively query trough nearly all model classes for generating a menu on the pages (and e.g. the User instance in request.identity).

I also tried expunging the objects while placing them in the tmpl_context/request, but that didn't work, too...
expire_on_commit=False is set, too, but since there's no commit, this doesn't work here, too.

Is there an argument against using session.rollback() in the tgext.crud error handler?
I mean, when I use session.commit(), I get an Exception saying that I should use transaction.commit() instead, but session.rollback() seems to work (and it apparently has a slightly different functionality).

Maybe percious could throw his two cents in... :D

Regards,
Moritz

Moritz Schlarb

unread,
Nov 13, 2012, 6:14:33 AM11/13/12
to sp...@googlegroups.com
I tagged Chris on the commit, so that he may take notice of the discussion! ;)

Alessandro Molina

unread,
Nov 13, 2012, 6:49:08 AM11/13/12
to sprox
Answered on the commit itself,
the main reason is that the transaction manager takes care of handling multiple transactions running, while session rollback would only rollback the current transaction. Causing issues when you are for example slicing your data.

I added an option to choose the desidered behaviour so that the app developer can choose whenever to rollback only the session used by the crud or the whole transaction.


To view this discussion on the web visit https://groups.google.com/d/msg/sprox/-/QNN9mN7mZzsJ.

Chris Perkins

unread,
Nov 13, 2012, 9:55:26 AM11/13/12
to sp...@googlegroups.com
Why aren't we doing a transaction.rollback() ?

-chris

To view this discussion on the web visit https://groups.google.com/d/msg/sprox/-/QNN9mN7mZzsJ.

Moritz Schlarb

unread,
Nov 13, 2012, 10:47:28 AM11/13/12
to sp...@googlegroups.com
At least my transaction doesn't have a rollback method! ;)

--
Moritz Schlarb

Alessandro Molina

unread,
Nov 13, 2012, 11:16:07 AM11/13/12
to sprox
rollback is the method name for SQLA, the transaction module uses the abort method.
Reply all
Reply to author
Forward
0 new messages