Giter Club home page Giter Club logo

Comments (3)

rodg avatar rodg commented on June 28, 2024 1

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.

rodg avatar rodg commented on June 28, 2024 1

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.

xkortex avatar xkortex commented on June 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.