I'm trying to build a custom SQLAlchemy type for storing UUIDs against
SQLAlchemy 0.2.8. However, my version of convert_bind_param isn't being
called, and consequently the values are being passed to the database in
a form not yet suitable. Sample code immediately below, exception
follows. What am I doing wrong?
=============================
#!/usr/bin/env python
import uuid # the python 2.5 uuid module; also works with python 2.4
import sqlalchemy
from sqlalchemy.ext.assignmapper import assign_mapper
from sqlalchemy.ext.sessioncontext import SessionContext
from sqlalchemy import Boolean, String, DateTime, ForeignKey, Table,
Column, create_session, func, mapper, polymorphic_union,
DynamicMetaData, TEXT
metadata = DynamicMetaData('test_tool')
session = create_session(bind_to=metadata.engine)
class UUID(String):
def __init__(self):
String.__init__(self, length=36)
def adapt(self, impltype):
return impltype(length=self.length)
def convert_bind_param(self, value, dialect):
if isinstance(value, uuid.UUID):
value = str(value)
elif isinstance(value, str):
value = str(uuid.UUID(value))
return super(UUID, self).convert_bind_param(value, dialect)
def convert_result_value(self, value, dialect):
return uuid.UUID(super(UUID, self).convert_result_value(value,
dialect))
test_table = Table('test', metadata,
Column('id', UUID, primary_key=True, default=uuid.uuid1),
Column('val', String))
class TestObj(object):
def __init__(self, value):
self.value = value
mapper(TestObj, test_table)
metadata.connect('sqlite:///:memory:')
metadata.create_all()
session.save(TestObj('foo'))
session.flush()
=============================
Traceback (most recent call last):
File "test_uuid_type.py", line 39, in ?
session.flush()
File ".../sqlalchemy/orm/session.py", line 234, in flush
self.uow.flush(self, objects, echo=self.echo_uow)
File ".../sqlalchemy/orm/unitofwork.py", line 207, in flush
flush_context.execute(echo=echo)
File ".../sqlalchemy/orm/unitofwork.py", line 377, in execute
head.execute(self)
File ".../sqlalchemy/orm/unitofwork.py", line 645, in execute
self._save_objects(trans)
File ".../sqlalchemy/orm/unitofwork.py", line 599, in _save_objects
task.mapper.save_obj(task.tosave_objects, trans)
File ".../sqlalchemy/orm/mapper.py", line 850, in save_obj
c = connection.execute(statement, params)
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
246, in execute
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
270, in execute_clauseelement
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
286, in execute_compiled
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
282, in proxy
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
321, in _execute_raw
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
340, in _execute
sqlalchemy.exceptions.SQLError: (InterfaceError) Error binding parameter
0 - probably unsupported type. 'INSERT INTO test (id, val) VALUES (?,
?)' [UUID('bddb5938-4a58-11db-b838-000ae66abc6c'), None]
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users