Correct way to declare an image field, sqlalchemy Correct way to declare an image field, sqlalchemy sqlite sqlite

Correct way to declare an image field, sqlalchemy


There is a library called SQLAlchemy-ImageAttach which provides a way to create entities having an image object. These could be exposed via some url.

Otherwise you can get a file object straight-forward via the SingleImageSet class.

With the lib you can choose from two of storages, such as the filesystem's or Amazon's S3. Inside the docs there is a described example (grey box) how to define previously the storage and then an User entity and owning a UserPicture entity.

from sqlalchemy import Column, ForeignKey, Integer, Unicodefrom sqlalchemy.orm import relationshipfrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy_imageattach.entity import Image, image_attachmentBase = declarative_base()class User(Base):"""User model."""    id = Column(Integer, primary_key=True)    name = Column(Unicode, nullable=False)    picture = image_attachment('UserPicture')    __tablename__ = 'user'class UserPicture(Base, Image):    """User picture model."""    user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)    user = relationship('User')    __tablename__ = 'user_picture'

I don't know Flask, but perhaps you can use a filestorage via some wsgi declaration together with the Lib here. I hope it helps you a bit and gives you some **.


I read in this post[1] that the best way is to declare the image field as a string to hold an url or link to the image, the image then can be stored in a external server, different from the server of your application. So if somebody have the same doubt just declare it as string and continue with your app development. A little disappointed of database management systems they only can be used to store numerical and alphabetical data(:p).

[1]https://www.reddit.com/r/flask/comments/31tywp/afbest_way_to_store_avatars_images_etc/