来自 sqlalchemy 的 psycopg2 register_composite [英] psycopg2 register_composite from sqlalchemy

查看:43
本文介绍了来自 sqlalchemy 的 psycopg2 register_composite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以以某种方式使用来自 psycopg2 的函数 register_composite, 当我使用 sqlalchemy 连接到 postgresql 数据库时?

is it possible to somehow use function register_composite from psycopg2, when i am using sqlalchemy to connect to postgresql database?

我的问题是我希望 SQLAlchemy 处理我在 postgresql 中创建的自定义复合类型,如下所示:

My problem is that I want SQLAlchemy to handle custom composite type that i created in postgresql like this:

CREATE TYPE card AS (value int, suit text);

Sqlalchemy 将这种类型的值作为字符串返回给我,我想了解一下我的新类型 sqlalchemy.如果在 SQL alchemy ORM 中找到了一些关于创建自定义复合类型的信息,但我只使用 SQLAlechemy Core API(而不是 ORM)并且 sqlalchemy IRC 上的某个人告诉我 psycopg 应该为我做这件事.

Sqlalchemy returns me values of this type as an string and I would like to somhow learn sqlalchemy my new type. If found some information about creating custom composite types in SQL alchemy ORM, but I am using just SQLAlechemy Core API (not ORM) and somebody on sqlalchemy IRC told me that psycopg should do it for me.

我很乐意接受任何可以解决此问题的建议.

I would be happy with any advice, that would solve this.

谢谢乔希

推荐答案

好的,我自己来回答 :)

Ok so, I will answer myself :)

我还没有找到如何从 sqlalchemy 核心 api 访问 psycopg2 的 register_composite 方法的任何方法.但是我可以通过 psycopg2 方法 new_type 和 register_adapter 注册新类型.可以在此处找到教程.此方法允许您注册从 sql 表示到 python 类的映射以及其他方式.

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.

从 SQLAlechemy 连接,您可以像这样访问这些方法:

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

这篇关于来自 sqlalchemy 的 psycopg2 register_composite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆