dahlia / sqlalchemy-imageattach

SQLAlchemy extension for attaching images to entities.
https://sqlalchemy-imageattach.readthedocs.io/
MIT License
116 stars 25 forks source link

Multiple files attached to an object (Reverse one to many from examples) #19

Closed mikeywaites closed 7 years ago

mikeywaites commented 10 years ago

Lets say I have a Model Event. An Event can have N attachments represented by EventAttachment model. All the logic seems to be suited to the one case where there is a FK from Event to the file attachment stored on EventAttachment. Obviously i could manage this manually but it seems all the logic for saving and working with files is wrapped up in the ImageSet query class. Am i missing something here?

class Event(db.Model):
    __tablename__ =  "event"
   id = Column(Integer, primary_key=True)

class EventAttachment(db.Model, BaseMixin, Image):
    __tablename__ = 'event_attachment'

    id = Column(Integer, primary_key=True)
    event_id = Column(Integer, ForeignKey('event.id'), nullable=False)

Ideally i want to have N EventAttchement instances with the logic found in the ImageSet class. Ie i can save multiple EventAttachments each with a file directly by doing something like

attachment = EventAttachment(event_id=1)
attachment.from_file(f)

Thanks.

dahlia commented 10 years ago

ImageSet is designed to have different sizes of the same image (for example, thumbnails for various DPIs). In order to make Event can have multiple EventAttachments you should have a more association table between them if we keep the current design of ImageSet.

eric-wieser commented 10 years ago

This should be fixable in sqlalchemy_imageattach by changing the database structure so that instead of having

Thing --<--+--<--Image(original=True)
           |
           +--<--Image
           |
           +--<--Image
           |
           +--<--Image

We have:

Thing --<--+--<--Image(id=1234, original_id = NULL)
           |     |
           ^     ^
           |     |
           |     +--<--+--Image(original_id = 1234)
           +-----|-----+
           |     |
           |     +--<--+--Image(original_id = 1234)
           +-----|-----+
           |     |
           |     +--<--+--Image(original_id = 1234)
           +-----------+

Ie, derived images reference both their original image and the thing in question

dahlia commented 7 years ago

Multiple image sets have been supported since SQLAlchemy-ImageAttach 1.0.0 (which was released about a year ago), therefore I close this issue.