full-stack-fastapi-template icon indicating copy to clipboard operation
full-stack-fastapi-template copied to clipboard

what data storage type should i use for image in SQLalchemy model?

Open pramadito opened this issue 4 years ago • 3 comments

from typing import TYPE_CHECKING

from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from app.db.base_class import Base

if TYPE_CHECKING:
    from .user import User  # noqa: F401


class HouseItem(Base):
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, index=True)
    description = Column(String, index=True)
    #image = ???                                                                                 #add here
    specification = Column(String, index=True)
    owner_id = Column(Integer, ForeignKey("user.id"))
    owner = relationship("User", back_populates="houseitems")

or should i import another library?

pramadito avatar May 05 '21 04:05 pramadito

LargeBinary probably if you are saving image content

haviduck avatar May 05 '21 23:05 haviduck

I think databases are not designed to function as file storage, maybe a better way is to save the image file to some location (like a local folder on your disk, AWS-S3 or Dropbox) the database would save a string with the location of the image file

for that purpose you can use: SQLAlchemy-ImageAttach is a SQLAlchemy extension for attaching images to entity objects

ieferrari avatar Aug 04 '21 17:08 ieferrari

I think databases are not designed to function as file storage, maybe a better way is to save the image file to some location (like a local folder on your disk, AWS-S3 or Dropbox) the database would save a string with the location of the image file

for that purpose you can use: SQLAlchemy-ImageAttach is a SQLAlchemy extension for attaching images to entity objects

storing binary representations is fine in some cases, but i would also go the bucket route if possible. no clue how a cdn would distribute bytea

haviduck avatar Aug 04 '21 21:08 haviduck