Comments (5)
seems like I'm making a mistake on my
@fastapi.post("/notes")
async def add_note_fastapi(note: NoteIn, raise_exc: bool = False):
as the equivalent starlette style includes the @transaction
decorator which I forgot to include:
@starlette.route("/notes_starlette", methods=["POST"])
@transaction
async def add_note_starlette(request):
"""
Create a note: Starlette style
"""
data = await request.json()
query = notes.insert().values(text=data["text"], completed=data["completed"])
await request.database.execute(query)
if "raise_exc" in request.query_params:
raise RuntimeError()
return JSONResponse({"text": data["text"], "completed": data["completed"]})
Now it leads to another issue but it's a step closer, I'll have to rewrite a decorator that suits FastAPI, or use the Depends system if I got it right ?
from fastapi.
I replaced the @transaction
that decorates the async def add_note_starlette(request):
with a
async with fastapi.error_middleware.app.backend.session().transaction():
within the fastapi route that creates a note solves the rollback issue for this test only.
I added TestStarletteStyleWithFastAPI
that uses the starlette route style (@app.route(path, methods=['POST'])
) decorators with a FastAPI instance and all tests pass, it's a lots of duplicate code but it's a beginning trying to use that DBMiddleWare
So my struggle here is really how you decorate a fastapi route with this @transaction from starlette : using an aysnc with
inside the route makes the rollback effective only during one test but subsequent tests fail.
from fastapi.
closing per #20 (comment)
from fastapi.
Thanks for coming back and closing it 👍
from fastapi.
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.
from fastapi.
Related Issues (20)
- No streaming interface can not support concurrency for two fastapi servers
- Using pydantic Json Type as Form data type doesn't work HOT 1
- Raw docstring (leading `r`) defeats form feed `\f` truncation HOT 3
- OpenAPI Example with multipart/form-data not showing up HOT 5
- [BUG] Using Nested Pydantic models and `params: MyModel = Depends()` forces OpenAPI docs GET methods to require a request body. HOT 6
- How to fix this bug? HOT 2
- [BUG] Upgrade python-mulipart==0.0.7 from low version fastapi upload file may be 400 HOT 4
- Use `RootModel` as query parameter HOT 2
- Context managers in `Depends` are broken after 0.106 HOT 16
- Middleware runs twice HOT 8
- Support for Pydantic deprecated fields HOT 1
- axios can't receive error response status code
- Potential footgun when using custom `Response(background=<Task()>)` in conjunction with injected `BackgroundTasks` - the custom response overwrites the tasks HOT 3
- lifespan
- Breaking change with path parameters when updating to pydantic>=2 from pydantic<2 HOT 1
- trying to live video stream using Fastapi
- Package on test.pypi.org is broken
- middleware type
- It throws an exception when I specify return Http status code
- When backend restart the frontend request a protected api cause an unhandle exception: Exception in ASGI application HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastapi.