Sqlalchemy Declaring Custom Userdefinedtype For Xml In Mssql
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 d
Solution 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
classXMLType(types.UserDefinedType):
defget_col_spec(self):
return'XML'defbind_processor(self, dialect):
defprocess(value):
if value isnotNone:
ifisinstance(value, str):
return value
else:
return etree.tostring(value)
else:
returnNonereturn process
defresult_processor(self, dialect, coltype):
defprocess(value):
if value isnotNone:
value = etree.fromstring(value)
return value
return process
Post a Comment for "Sqlalchemy Declaring Custom Userdefinedtype For Xml In Mssql"