I am building an App by using Flask with Postgress and SQLAlchemy. There is a table column where I need to store serialized data (JSON). How do I define the field in the model class?

Can I just use string field for the column?

from flask.ext.sqlalchemy import SQLAlchemy
from main import app

db = SQLAlchemy(app)

class Fame (db.Model):
    __tablename__ = "toto"
    id = db.Column('id', db.Integer, primary_key=True)
    data = db.Column('data', db.JSON)
    creation_date = db.Column('creation_date', db.Date, default=datetime.utcnow)

But I got this error:

AttributeError: 'SQLAlchemy' object has no attribute 'JSON'

share|improve this question

You need to import JSON or JSONB from sqlalchemy.dialects.postgresql

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.