dahlia / sqlalchemy-imageattach

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

Error generating thumbnails #23

Closed fokoenecke closed 10 years ago

fokoenecke commented 10 years ago

Hi! I am trying to generate thumbnails for my attached images (v.0.8.1). Unfortunately I am getting 'NotImplementedError: object_id property has to be implemented' when calling .locate() on them. I tried everything in the documentation and debugged through the code, but could not figure out why this happens.

My code looks like this:

class UserPicture(Base, BaseModel, Image):
    user_data_id = Column(Integer, ForeignKey('user_data.id'), primary_key=True)
    user_data = relationship(UserData)
    __tablename__ = 'user_picture'
from sqlalchemy_imageattach.context import store_context

def my_method():
    with store_context(store):
        try:
            thumb = user_data.picture.find_thumbnail(width=150)
        except NoResultFound:
            thumb = user_data.picture.generate_thumbnail(width=150)
        thumb_location = thumb.locate()

I did not change anything on my primary key and it is found in the object_id property, but

pk_value = getattr(self, pk[0])

fails to get a value.

dahlia commented 10 years ago

I did not change anything on my primary key and it is found in the object_id property, but

pk_value = getattr(self, pk[0])

fails to get a value.

Do you mean it raises AttributeError, or returns None?

fokoenecke commented 10 years ago

It returns None. pk[0] is my 'user_data_id'.

dahlia commented 10 years ago

It might be related to Session’s autoflush and autocommit settings. How are your settings?

fokoenecke commented 10 years ago

You were right! I am using the default on both values, so I guess autoflush is enabled and autocommit is disabled. I tried to flush manually before calling .locate() and it works:

DBSession.flush()
thumb_location = thumb.locate()

This also explains a behaviour i didn't understand earlier. Many thanks for your help!