Comments (3)
Yeah I just ran into this as well, going to +1 having the option to just raise the errors up. I get the point mentioned in other issues (like #66 ), but at the same time I find this package useful for stuff that never sees the light of day (quick tests, internal tools) so having a bonus option on the create route to have it just print the real error would be very helpful. If we could just toggle a boolean to have the "less safe but more useful" errors that would be a good change.
Something more ideal might be having the ability to pass in an error handling function to the router? I don't think it'd be 100% necessary but nice error handling customization would be cool IMO.
from fastapi-crudrouter.
In the mean time this little hack seems to work (not exhaustively tested):
from typing import Callable, Any
from fastapi_crudrouter import SQLAlchemyCRUDRouter as _SQLACRUDRouter
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import DeclarativeMeta as Model
from sqlalchemy.exc import IntegrityError
from fastapi import Depends
CALLABLE = Callable[..., Model]
class SQLAlchemyCRUDRouter(_SQLACRUDRouter):
def _create(self, *args: Any, **kwargs: Any) -> CALLABLE:
def route(
model: self.create_schema, # type: ignore
db: Session = Depends(self.db_func),
) -> Model:
try:
db_model: Model = self.db_model(**model.dict())
db.add(db_model)
db.commit()
db.refresh(db_model)
return db_model
except IntegrityError as e:
db.rollback()
self._raise(e)
return route
from fastapi-crudrouter.
I just got bit by this. I was wracking my brain trying to figure out how I could be getting Key already exists
errors with a completely empty database. So I debugged the _create
function and found out the error it was eating was actually
sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "created_on" violates not-null constraint
(I had forgot to set server_default=sa.sql.func.now()
for the column).
I agree, doing a self._raise(e)
just like the _update
method would be an easy fix here.
from fastapi-crudrouter.
Related Issues (20)
- issue with related field from tortoise orm pydantic schema
- Structural error for 404 HTTPException
- Use poetry for dependency management HOT 2
- Insert object with Many-to-many relatioship via sqlalchemy HOT 2
- Support piccolo orm
- Add the possibility to customize the "item_id" parameter name HOT 1
- Are you considering adding authentication/authorization in the future? HOT 2
- All ORMs/backends should allow partial updates
- Allow objects in attributes for create/update routes (sqlalchemy)
- Outdated contribution documentation HOT 1
- Overriding GET route does not properly remove existing route
- Pydantic V2 Support HOT 1
- Allow providing additional parameters to FastAPIs add_api_route() function
- Pydantic V2 incompatibility: FieldInfo object does not have the attribute 'type_' HOT 4
- AttributeError: 'FieldInfo' object has no attribute 'name' HOT 3
- mypy refuses to check
- When Class View is supported
- What is the development status? 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-crudrouter.