MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.88k stars 399 forks source link

no binary format encoder for type jsonb #1144

Open koshak01 opened 5 months ago

koshak01 commented 5 months ago

,asyncpg.exceptions._base.InternalClientError: no binary format encoder for type jsonb (OID 3802)

async def insert_binary(self, table, rows):
    if rows:
        rows = common_function.convert_data_type(rows, decimal.Decimal, float, )
        columms = rows[0].keys()
        data = []
        for row_index, rows_once in enumerate(rows):
            data_once = [rows_once[columns_once] for columns_once in columms]
            data.append(tuple(data_once))
        conn = await self.connect()
        await conn.copy_records_to_table(table,records=data, columns=rows[0].keys())
        await conn.fetch('SELECT 1')
        await conn.close()

I've tried various methods, but I couldn't manage to insert binary data. I've tried using decoders and other approaches. It seems like it needs a binary decoder, not a text one, but I haven't figured out how to represent it in binary. I'd appreciate any help.


await conn.set_type_codec(
    "jsonb",
    encoder=lambda data: b"\x01" + common_function.decoder.dumps(data, False),
    decoder=lambda data: common_function.decoder.loads(data[1:]),
    schema="pg_catalog",
    format="binary"
)
await conn.set_type_codec(
    'numeric'
    , encoder = lambda f: str(f).encode('utf8')
    , decoder = lambda b: b.decode('utf8')
    , schema='pg_catalog'
    , format='binary'
)

"This also doesn't work."

"I replaced 'numeric' with 'float' (database field types), and the problem 'no binary format encoder for type numeric' disappeared. However, I couldn't figure out which format decoder is needed for JSON."