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 filter by FileType #43

Open
tyyrok opened this issue Jan 25, 2024 · 0 comments
Open

SQLAlchemy filter by FileType #43

tyyrok opened this issue Jan 25, 2024 · 0 comments

Comments

@tyyrok
Copy link

tyyrok commented Jan 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant