LuisLuii / FastAPIQuickCRUD

Generate CRUD methods in FastApi from an SQLAlchemy schema
https://pypi.org/project/fastapi-quickcrud/
MIT License
255 stars 32 forks source link

Support for PostgreSQL data types #12

Closed filipmarkoski closed 2 years ago

filipmarkoski commented 2 years ago

I created a few SQLAlchemy models containing some PostgreSQL data types such as from sqlalchemy.dialects.postgresql import BYTEA, INTERVAL, JSONB, UUID

and I encountered the following exception fastapi_quickcrud.misc.exceptions.ColumnTypeNotSupportedException: The type of column Blob (BYTEA) not supported yet

I haven't tested the other types except for Postgres BYTEA, however, there might be an issue for from sqlalchemy import ARRAY as well.

I want to build a ReactJS app in which I can upload a photo via a FastAPI endpoint and store it in the BYTEA field. Afterwards, I would also like to have a FastAPI endpoint that lists all of the BYTEA entries, which I can use to make a gallery in ReactJS.

class Photo(Base):
    __tablename__ = 'photo'
    __table_args__ = {'schema': 'dbo'}

    ID = Column(BigInteger,
                Identity(always=True, start=1, increment=1, minvalue=1, maxvalue=9223372036854775807, cycle=False,
                         cache=1), primary_key=True)
    UUID_ = Column('UUID', UUID, nullable=False, server_default=text('gen_random_uuid()'))
    DateCreated = Column(DateTime(True), nullable=False, server_default=text('CURRENT_TIMESTAMP'))
    DateModified = Column(DateTime(True), nullable=False, server_default=text('CURRENT_TIMESTAMP'))
    IsActive = Column(Boolean, nullable=False, server_default=text('true'))

    Blob = Column(BYTEA, nullable=False)

    ...

PhotoCRUDRouter = crud_router_builder(db_model=Photo,
                                      db_session=get_transaction_session,
                                      prefix='/quote', tags=['Photo'],
                                      exclude_columns=['UUID','DateCreated','DateModified','IsActive'],
                                      async_mode=True,
                                      autocommit=False,
                                      sql_type=SqlType.postgresql,
                                      )
app.include_router(PhotoCRUDRouter)
LuisLuii commented 2 years ago

Sorry. This package only support the following type UUID int float Decimal date time datetime timedelta bool dict list

And I think usually more efficient and easier to store images in the filesystem and reference them via an URI stored in the database.

More data types may be supported in the future. But there are no development plans for this yet and will update here

filipmarkoski commented 2 years ago

What about file uploads? https://fastapi.tiangolo.com/tutorial/request-files/