Replies: 1 comment 2 replies
-
Thank you @mixa2130 for the detailed explanation. There are several things I don't get though:
Can you add more details about this? If this is confirmed, it looks like the first thing to fix. Now, regarding the
So, when you're inside your router logic, the session has already begun (because FastAPI Users indeed make a call to query the user). If you need to have another level, you can set the
|
Beta Was this translation helpful? Give feedback.
-
Hello guys!
I'm new at fastapi-users, so pls don't throw slippers at me...
Working with this lib I've found out an interesting problem: in the endpoint where there is a dependency on
fastapi_users.current_user
I can't isolate my transactions without making an extra commit before.Getting
InvalidRequestError: A transaction is already begun on this Session.
error atAccording to sqlalchemy docs:
I think it would be great to add extra
session.commit()
at functions which works with the dbI'll explain why this seems like a good idea to me.
For instance, there is a bug in the new version of fastapi-users lib that makes some extra objects in db. There are no commits at the end of functions in
BaseUserManager
. So this objects would be created with the first of my commits in the endpoint. Same story about rollback. Sounds not cool, isn't it?Making extra commit at the highest level of abstraction allows us to separate user transactions with the lib ones. Even if anything will go wrong - it would be more easier to find-out where the problem comes: lib or endpoint code.
Ok, time to code 🐢
To test error
take an example from the website, create basic config
Basic endpoint:
Without making
session.commit()
beforeasync with session.begin():
you won't be able to isolate your transaction.To fix the problem
Experimenting with the lib code I've found out that this problem has a very simple decision: just add
session.commit()
at the end of eachBaseUserManager(Generic[models.UP, models.ID])
functions:I'm ready to fix this problem with pull request which fully passes the tests, but don't know is it really a good idea for all other people. Maybe smth isn't right with me
Beta Was this translation helpful? Give feedback.
All reactions