On PUT-Request respond with updated resource-data - commit(), transaction manager

27 views
Skip to first unread message

benko...@gmail.com

unread,
Apr 3, 2017, 3:50:56 PM4/3/17
to pylons-discuss
Hello!

I'm working on a project where I use Pyramid together with Cornice to provide a REST-API. When I do a PUT-request, I would like to return the updated resource in the Response. But as the COMMIT only happens after returning the Response and request.dbession.commit() is not allowed in the default configuration (SQLAlchemy-scaffold - "Transaction must be committed using the transaction manager"), I'm wondering how I could return the resource with the latest DB-State after updating it with the PUT-payload.
Using a GET right after a successful PUT is my current solutions, but that feels like an unclean workaround.

Is there a way to do this without replacing the whole transaction-mechanism, which seems complex?

Kind regards,
Christian

Mikko Ohtamaa

unread,
Apr 3, 2017, 3:54:28 PM4/3/17
to pylons-...@googlegroups.com
Hi Christian,

I'm working on a project where I use Pyramid together with Cornice to provide a REST-API. When I do a PUT-request, I would like to return the updated resource in the Response. But as the COMMIT only happens after returning the Response and request.dbession.commit() is not allowed in the default configuration (SQLAlchemy-scaffold - "Transaction must be committed using the transaction manager"), I'm wondering how I could return the resource with the latest DB-State after updating it with the PUT-payload.
Using a GET right after a successful PUT is my current solutions, but that feels like an unclean workaround.

Is there a way to do this without replacing the whole transaction-mechanism, which seems complex?

If I read this correctly this is not complex at all.

Run:

request.dbsession.flush()

 

Kind regards,
Christian

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/bf41d803-9375-445e-a903-a767d507bab9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

benko...@gmail.com

unread,
Apr 4, 2017, 11:23:49 AM4/4/17
to pylons-discuss
On Monday, 3 April 2017 21:54:28 UTC+2, Mikko Ohtamaa wrote:
Hi Christian,

I'm working on a project where I use Pyramid together with Cornice to provide a REST-API. When I do a PUT-request, I would like to return the updated resource in the Response. But as the COMMIT only happens after returning the Response and request.dbession.commit() is not allowed in the default configuration (SQLAlchemy-scaffold - "Transaction must be committed using the transaction manager"), I'm wondering how I could return the resource with the latest DB-State after updating it with the PUT-payload.
Using a GET right after a successful PUT is my current solutions, but that feels like an unclean workaround.

Is there a way to do this without replacing the whole transaction-mechanism, which seems complex?

If I read this correctly this is not complex at all.

Run:

request.dbsession.flush()

And SQLAlchemy will sync all in-memory objects from the database.

While I already had flush() in place wherever necessary, your answer made me take a closer look at my model. I realized that I'm creating my JSON-Response from persisted, therefore outdated objects, not the newly created ones - that's why I didn't see my updates. Thanks for pointing me in the right direction!



Reply all
Reply to author
Forward
0 new messages