相关疑难解决方法(0)

SQLAlchemy TypeDecorator不起作用

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

所以我让XMLType课堂沟通xml.etree,但它没有按照我的意愿工作.

这是我写的代码:

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
Run Code Online (Sandbox Code Playgroud)

它适用于检索值和结果处理.但是当我试图插入一行时,我得到一个错误(当然,我把bodyas xml.etree.ElementTree.Element对象):

IntegrityError: (IntegrityError) null value in column "body" violates not-null 
constraint "INSERT INTO comments (id, author_id, look_id, body, …
Run Code Online (Sandbox Code Playgroud)

xml postgresql sqlalchemy elementtree

5
推荐指数
1
解决办法
911
查看次数

标签 统计

elementtree ×1

postgresql ×1

sqlalchemy ×1

xml ×1