0

I want to use sqlalchemy ORM (currently version 1.0.12) to save pictures in a Postgres database. My Python code is as follows:

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Binary
from sqlalchemy.orm import sessionmaker

engine = create_engine('postgresql://127.0.0.1:54321/warehouse', echo=True)
Session = sessionmaker(bind=engine)

Base = declarative_base()

class Picture(Base):

    __tablename__ = 'pictures'

    picture_id = Column(Integer(), primary_key=True)
    data = Binary()
    filename = Column(String())

Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

My problem is that the binary column data is not created which one can see with psql or in the log of the previous script:

2016-05-11 13:07:12,881 INFO sqlalchemy.engine.base.Engine CREATE TABLE pictures (
        picture_id SERIAL NOT NULL,
        filename VARCHAR,
        PRIMARY KEY (picture_id) )

Did I miss something ?

1 Answer 1

4

Yes, you missed something: the Column(...) call.

Try

class Picture(Base):

    __tablename__ = 'pictures'

    picture_id = Column(Integer(), primary_key=True)
    data = Column(Binary())    # <---
    filename = Column(String())

instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.