2011-12-16 57 views
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 

它运作良好,在检索值和结果处理。但是当我试图插入行,我得到一个错误(当然,我把bodyxml.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} 

眼见bodyNone,很明显的是,结合处理器不工作对,但我认为我已经实施了,所以我不知道该怎么做才能改变这种状况。

process_bind_param给我同样的错误。

我的代码在哪里出错?

回答

4

​​函数将XML转换为流(默认为stdout)并返回None。使用ElementTree.tostring()或将其转储到StringIO对象。

+0

谢谢。这是我的无知,而不是SQLAlchemy的问题。 :-) – yoloseem 2011-12-16 14:50:40