-
-
Notifications
You must be signed in to change notification settings - Fork 162
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
Comments
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? 🚀 |
@jeanlst Do you have a workaround for this? Thanks |
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()
fastapi_crudrouter used as query parameter e.g. in _get_one -> self._pk_type which I don't understand in detail. 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. |
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. One of them is from @tiangolo the creator of fastapi. -> Link |
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?
The text was updated successfully, but these errors were encountered: