Skip to content Skip to sidebar Skip to footer

Psycopg2 Register_composite From Sqlalchemy

is it possible to somehow use function register_composite from psycopg2, when i am using sqlalchemy to connect to postgresql database? My problem is that I want SQLAlchemy to handl

Solution 1:

You can use the psycopg2 function register_composite from sqlalchemy but you have to

import psycopg2.extras

yourself, then with your connection object c from

c = e.connect()

just do

psycopg2.extras.register_composite('card', c.connection, globally=True)

You have to use c.connection because

>>> hasattr(c.connection, 'cursor')
True

Solution 2:

Ok so, I will answer myself :)

I have not found any way how to access to method register_composite of psycopg2 from sqlalchemy core api. But i am able to register new type via psycopg2 methods new_type and register_adapter. Tutorial can be found here. This methods allows you to register mapping from sql representation to python class and the other way.

From SQLAlechemy connection you can access these methods like this:

from sqlalchemy import *
e = create_engine("postgresql://xxx:xxx@localhost:5432/db")
c = e.connect()
c.dialect.dbapi.extensions.register_adapter
c.dialect.dbapi.extensions.new_type

Post a Comment for "Psycopg2 Register_composite From Sqlalchemy"