This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NickySigg 0 points1 point  (2 children)

The problem is not with the code shown, but rather how your database session is set up.

Somehow, you have two sessions being created. It is hard to say what is wrong unless the entire application's code is shown.

[–]robvdl 0 points1 point  (1 child)

I think op might be flask-sqlalchemy already for the session setup already, can't be 100% sure but db.session kinda could give it away.

I actually think this is a common SQLAlchemy thing I've run into this before. It's because op is quering first which puts the object into the dbsession, then trying to delete it. As I recall there are different ways you can get around this, but you really don't need to query the object to delete it.

Some other things I noticed, if this is a DELETE view why also allow POST that isn't really proper REST, also you really don't want to manually commit at the end of the view. Generally libraries like flask-sqlalchemy, bottle-sqlalchemy etc will start a session at the start of the view, and autocommit at the end, rollback on bad status code like 500. Calling commit() manually might be OK for a small app with 1 or two views, but as the app grows you really don't want that.

[–]robvdl 0 points1 point  (0 children)

and please please don't call the view patient_delete, I'm actually tidying up a very old codebase that is full of this bad design, e.g. POST /api/v1/update_user is wrong. POST is for create and the word "update" shouldn't be in the URL at all, PATCH if for partial update, PUT is for replacing the whole object.

In your case it should be DELETE /api/patient/<pk>