-
I just started implementing the SQL Router for a small project I'm working on and love the concept. I'm running into a few issues with creating nested models and the way I've implemented the database 'interface' class. Basically, I'd like to extend the SQL Alchemy router to overwrite some of the route generation. Something like this... class MyCustomerCRUDRouter(SQLAlchemyCRUDRouter):
def _create(self, *args: Any, **kwargs: Any) -> Callable[..., Model]:
def route(
model: self.create_schema, # type: ignore
db: Session = Depends(self.db_func),
) -> Model:
try:
db_model: Model = some_custom_create_func()
return db_model
except IntegrityError:
db.rollback()
raise HTTPException(422, "Key already exists")
return route
def _update(self, *args: Any, **kwargs: Any) -> Callable[..., Model]:
def route(
item_id: self._pk_type, # type: ignore
model: self.update_schema, # type: ignore
db: Session = Depends(self.db_func),
) -> Model:
try:
db_model: Model = some_custom_update_func()
return db_model
except IntegrityError as e:
db.rollback()
raise HTTPException(422, ", ".join(e.args))
return route Is that a use case of the library or am I playing with fire? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@hay-kot I believe this should work for your intended use case. Another option would be to just override the routes (docs: https://fastapi-crudrouter.awtkns.com/routing) but that would need to be done per router (not sure how many you are creating). |
Beta Was this translation helpful? Give feedback.
@hay-kot I believe this should work for your intended use case. Another option would be to just override the routes (docs: https://fastapi-crudrouter.awtkns.com/routing) but that would need to be done per router (not sure how many you are creating).