Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLALchemy support for Composite Primary Key/Different index #133

Open
jeanlst opened this issue Jan 6, 2022 · 4 comments
Open

SQLALchemy support for Composite Primary Key/Different index #133

jeanlst opened this issue Jan 6, 2022 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jeanlst
Copy link

jeanlst commented Jan 6, 2022

I was looking at the implementation for SQLAlchemy and noticed that it takes only the first primary key of the table

https://github.com/awtkns/fastapi-crudrouter/blob/master/fastapi_crudrouter/core/sqlalchemy.py#L50

What if I have a composite primary key in my table?

@jeanlst jeanlst changed the title SQLALchemy support for Composite Primary Key SQLALchemy support for Composite Primary Key/Different index Jan 6, 2022
@awtkns awtkns added enhancement New feature or request good first issue Good for newcomers labels Jan 12, 2022
@awtkns
Copy link
Owner

awtkns commented Jan 12, 2022

Hi @jeanlst. Thanks for bringing this up.

You are right! This is something that is currently not possible with the SQLA implementation. Is this something that you would be interested in PRing? 🚀

@AthreyVinay
Copy link

@jeanlst Do you have a workaround for this? Thanks

@everthought
Copy link

I had already started writing a simple auto generator for Fastapi. Then I came across this project.

I created an ItemIndex pydantic class for this. This represents the primarykeys. I then use this in the function with the addition = Depends()

@self.router.delete("", response_model=Message)
def delete_product(id: ItemIndex = Depends(), db: Session = Depends(db_generator)) -> Any:

fastapi_crudrouter
image

fastapi_crudrouter used as query parameter e.g. in _get_one -> self._pk_type which I don't understand in detail.
Sqlalchemy also accepts a dic in .get(). which in turn can be passed to Sqlalchemy from the pydantic request model with .dict().

image
image

If you take the primary keys using the sqlalchamy model, you can certainly do this without an extra pydantic model.

Maybe it's something for a change request.

@everthought
Copy link

I took a closer look at fastapi_crudrouter.

        self._pk: str = db_model.__table__.primary_key.columns.keys()[0]
        self._pk_type: type = _utils.get_pk_type(schema, self._pk)

In the init of the SQLAlchemyCRUDRouter class the SQLalchemy model is actually used to read the primary key. Unfortunately, only the first entry from the list is passed as an attribute in _pk. So it would be possible to extract the necessary primary key here. These would then have to be dynamically transferred to the routes. I can't find any reference to this in the fastapi documentation. I'm not really familiar with pydantic yet.
There are some libraries that translate the SQLalchemy models into Pydantic schemas.

One of them is from @tiangolo the creator of fastapi. -> Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants