4
我在我的postgresql数据库中使用xml
,我需要一个自定义类型可以处理SQLAlchemy中的xml
数据。SQLAlchemy TypeDecorator不起作用
所以我做了XMLType
班与xml.etree
沟通,但它不工作,因为我希望。
here`s,我写的代码:
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
为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
给我同样的错误。
我的代码在哪里出错?
谢谢。这是我的无知,而不是SQLAlchemy的问题。 :-) – yoloseem 2011-12-16 14:50:40