Search code examples
python-3.xsqlalchemyjsonb

SQLAlchemy: column_property jsonb operation


My Test table has a JSONB column data:

class Test(Base):
    __tablename__ = 'test'
    data = Column(JSONB)

A typical document has two lists:

{'percentage': [10, 20, 50, 80, 90],
'age': [1.21, 2.65, 5.23, 8.65, 11.78]
}

With a column_property I would like to tailor this two lists so it is available as a dictionary. In "open field" Python this is straightforward:

dict(zip(Test.data['percentage'], Test.data['age']))

But with a column_property:

Test.data_dict = column_property(
    dict(zip(Test.data['percentage'], Test.data['age']))
)

this gives:

AttributeError: 'dict' object has no attribute 'label'

Is this actually possible and how should this been done?


Solution

  • Does it solves your problem?

    @property
    def data_dict(self):
        return dict(zip(Test.data['percentage'], Test.data['age']))