I am using the core SQLAlchemy library to access some PostgreSQL database. I have the following table:
create table foo (j jsonb);
And the following python code:
from decimal import * from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, DateTime from sqlalchemy.dialects.postgresql import JSONB metadata = MetaData(schema="public") foo = Table('foo', metadata,Column('f', JSONB)) d = Decimal(2) ins = foo.insert().values(j = {'d': d})
The last sentence fails with the following error:
StatementError("(builtins.TypeError) Decimal('2') is not JSON serializable",)
That's why I ask this question: is there a way to specify a custom encoder for SQLAchemy to use when encoding json data in a PostgreSQL dialect?
source share