Skip to content

SQLAlchemy filter by FileType #43

@tyyrok

Description

@tyyrok

Hello guys,
I haven't found any info about this error in web, so writing here...

Some days ago I tried to make a query in SQLAlchemy and filter FileType field by file extension. So I had this error:

   File "/usr/local/lib/python3.11/site-packages/fastapi_storages/integrations/sqlalchemy.py", line 44, in process_bind_param 
check-point-core   |     if len(value.file.read(1)) != 1:
check-point-core   |            ^^^^^^^^^^
check-point-core   | AttributeError: 'str' object has no attribute 'file'

My models:

class Job(Base):
    attachments: Mapped[list["JobAttachment"]] = relationship(
        "JobAttachment",
        back_populates="job",
        lazy="selectin",
        cascade="all, delete",
        passive_deletes=True,
    )
class JobAttachment(Base):
    job_id: Mapped[Optional[int]] = mapped_column(
        Integer, ForeignKey("jobs.id", ondelete="CASCADE")
    )
    job: Mapped["Job"] = relationship("Job", back_populates="attachments")

    content: Mapped[Optional[FileType]] = mapped_column(
        FileType(storage=a_jobs_storage), nullable=True
    )

I use FileType from

from fastapi_storages.integrations.sqlalchemy import FileType

And my query something like this:

            select(
                Job.id,
                func.count().label("attachment_count")
            )
            .select_from(Job)
            .join(JobAttachment, Job.attachments)
            .filter(
                or_(
                    JobAttachment.content.endswith(".jpg"),
                    JobAttachment.content.endswith(".png"),
                    JobAttachment.content.endswith(".jpeg"),
                )
            )
            .group_by(Job.id)
            .alias()

For storage I use from S3Storage.

Everything works when I manipulate with results of query but in filter there is this error.
Is it possible to use FileType field in this way?

P.S. Also I tried to filter by content.path but it's unavailable in filter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions