Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml SQLAlchemy TypeDecorator不';行不通_Xml_Postgresql_Sqlalchemy_Elementtree - Fatal编程技术网

Xml SQLAlchemy TypeDecorator不';行不通

Xml SQLAlchemy TypeDecorator不';行不通,xml,postgresql,sqlalchemy,elementtree,Xml,Postgresql,Sqlalchemy,Elementtree,我正在postgresql数据库中使用xml,我需要一个自定义类型,可以在SQLAlchemy中处理xml数据 因此,我创建了一个与xml.etree通信的XMLType类,但它并没有像我希望的那样工作 这是我写的代码: import xml.etree.ElementTree as etree class XMLType(sqlalchemy.types.TypeDecorator): impl = sqlalchemy.types.UnicodeText type = e

我正在postgresql数据库中使用
xml
,我需要一个自定义类型,可以在SQLAlchemy中处理
xml
数据

因此,我创建了一个与
xml.etree
通信的
XMLType
类,但它并没有像我希望的那样工作

这是我写的代码:

import xml.etree.ElementTree as etree

class XMLType(sqlalchemy.types.TypeDecorator):

    impl = sqlalchemy.types.UnicodeText
    type = etree.Element

    def get_col_spec(self):
        return 'xml'

    def bind_processor(self, dialect):
        def process(value):
            if value is not None:
                return etree.dump(value)
            else:
                return None
        return process

    def process_result_value(self, value, dialect):
        if value is not None:
            value = etree.fromstring(value)
        return value
它在检索值和结果处理方面效果很好。但是当我尝试插入一行时,我得到了一个错误(当然,我将
主体
放为
xml.etree.ElementTree.Element
对象):

看到
body
值为
None
,显然绑定处理器工作不正常,但我认为我正确地实现了这一点,因此我不知道该如何改变这种情况

process\u bind\u param
给我同样的错误


我的代码哪里出错了?

ElementTree.dump()
函数将XML转储到流(默认情况下为标准输出),并返回
None
。使用
ElementTree.tostring()
或将其转储到
StringIO
对象。

谢谢。这是我的无知,不是炼金术的问题。:-)哦,我不知道有“接受答案”功能。我就这么做了。谢谢你的考虑。
IntegrityError: (IntegrityError) null value in column "body" violates not-null 
constraint "INSERT INTO comments (id, author_id, look_id, body, created_at) 
VALUES (nextval('object_seq'), %(author_id)s, %(look_id)s, %(body)s, now()) 
RETURNING comments.id" {'body': None, 'author_id': 1550L, 'look_id': 83293L}