dev-jim
dev-jim

Reputation: 2524

storing serialized data in postgres by SQLAlchemy

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'

Upvotes: 2

Views: 1532

Answers (1)

RazerM
RazerM

Reputation: 5492

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

Upvotes: 2

Related Questions