Search code examples
pythonsqlalchemymodelfastapi

Save multiple media pictures for a value in database


I have a Table called property that accepts a contains column which refers to propertyContains table, then propertyContains will have a media column which refers to media table. i have no idea if i am storing the images the right way and i am wondering if there is any better/more efficient ways.

my code


class Property(Base):
    __tablename__ = "property"

    property_id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("propertyOwner.user_id"))
    title = Column(String)
    description = Column(String)

    Location = Column(String)
    rented = Column(Boolean)
    rented_by = Column(Integer, ForeignKey("client.client_id"))

    contains = relationship("PropertyContains", back_populates="contains_owner")
    owner = relationship("Owner", back_populates="properties")

    date = Column(Date, default=func.now())

    class Config:
        arbitrary_types_allowed = True




class Media(Base):
    __tablename__ = "media"
    media_id = Column(Integer, unique=True, primary_key=True, index=True)


    media1 = Column(LargeBinary)
    media2 = Column(LargeBinary)
    media3 = Column(LargeBinary)

    media_owner = Column(Integer, ForeignKey('propertyContains.property_contains_id', ondelete="CASCADE"))

    class Config:
        arbitrary_types_allowed = True

class PropertyContains(Base):
    __tablename__ = "propertyContains"
    property_contains_id = Column(Integer, unique=True, primary_key=True, index=True)
    property_id = Column(Integer, ForeignKey("property.property_id"))
    # media_id = Column(Integer, ForeignKey("media.media_id"))
    bed_rooms = Column(Integer)
    media = relationship("Media", backref="media", passive_deletes=True)

    contains_owner = relationship("Property", back_populates="contains")

    class Config:
        arbitrary_types_allowed = True

please keep in note that i am a beginner <3.


Solution

  • Turns out the good practice is to just save the image in your server and save the path.