1

I am trying to declare a custom type of "XMLType" for use with MSSQL. But I keep getting 'AttributeError: module 'app.db.XMLType' has no attribute '_set_parent_with_dispatch''. I don't see anything that isn't declared correctly in my XMLType class file.

import sqlalchemy.types as types

from lxml import etree

class XMLType(types.UserDefinedType):

    def get_col_spec(self):
        return 'XML'

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

    def result_processor(self, dialect, coltype):
        def process(value):
            if value is not None:
                value = etree.fromstring(value)
            return value
        return process

I am basing the code off of http://docs.sqlalchemy.org/en/latest/core/custom_types.html?highlight=get_col_spec#sqlalchemy.types.UserDefinedType and Using postgresql xml data type with sqlalchemy.

Community
  • 1
  • 1
Kevin Vasko
  • 1,561
  • 3
  • 22
  • 45
  • Please provide a) full tracebacks b) proper minimal, complete and verifiable example. Where and how were you using your custom type when you got the error? – Ilja Everilä Apr 11 '17 at 11:02

1 Answers1

1

So there happened to be something wrong with my import. For my model I originally had the following:

from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
import conductor.db.XMLType as XML

Base = declarative_base()

class Raw_data_records(Base):
    __tablename__ = 'raw_data_records'

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    basename = Column('basename', String)
    filename = Column('filename', String)
    file_size = Column('file_size', Integer)
    machine = Column('machine', String)
    insert_timestamp = Column('insert_timestamp', DateTime)
    raw_xml = Column('raw_xml', XML)

I then changed my import to:

from spcconductor.db.XMLType import XMLType and the raw_xml = Column('raw_xml', XML) to raw_xml = Column('raw_xml', XMLType) and it worked.

For anyone that needs the information this is the XMLType.py (I have not tested it...):

import sqlalchemy.types as types

from lxml import etree

class XMLType(types.UserDefinedType):

    def get_col_spec(self):
        return 'XML'

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

    def result_processor(self, dialect, coltype):
        def process(value):
            if value is not None:
                value = etree.fromstring(value)
            return value
        return process
Kevin Vasko
  • 1,561
  • 3
  • 22
  • 45