我在用着xml
在我的 postgresql 数据库中,我需要一个可以处理的自定义类型xml
SQLAlchemy 中的数据。
所以我做了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
它在检索值和结果处理方面效果很好。但是当我尝试插入一行时,出现错误(当然,我把body
as xml.etree.ElementTree.Element
目的):
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}
看到那body
值为None
,很明显绑定处理器无法正常工作,但我认为我正确实现了它,所以我不知道应该做什么来改变这种情况。
process_bind_param
给了我同样的错误。
我的代码哪里出错了?