Thanks for the response. I am actually trying to do an ARRAY(INET) here,
and while pscopg2 does have a register_inet() it does not seem to (fully?)
work with arrays. The return casting works fine, but I get type "is of
type inet[] but expression is of type text[]" when trying to bind.
The fix you posted does indeed work great for UUID. Oddly, I'm still
getting that type casting error when I try with INET.
class INET_ARRAY(TypeDecorator):
impl = ARRAY(INET, dimensions=1)
def bind_expression(self, bindvalue):
val = bindvalue.value
if val is None:
val = []
elif not hasattr(val, '__iter__'):
return bindvalue
return array(
cast(literal(str(inet_val)), INET())
for inet_val in val
)
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (ProgrammingError) column "inets" is of
type inet[] but expression is of type text[]
LINE 1: ... (('2013-01-07T15:05:39.623316'::timestamp), (3), ARRAY['192...
^
HINT: You will need to rewrite or cast the expression.
'INSERT INTO example_2 (timestamp, num, inets) VALUES (%(timestamp)s,
%(num)s, %(inets)s)' {'inets': ['192.168.1.1', '192.168.1.2'], 'timestamp':
(datetime.datetime(2013, 1, 7, 15, 5, 39, 623316),), 'num': (3,)}
I will continue digging into this as I would have expected this to work
like the UUID example.
Thanks,
Hans
On Monday, January 7, 2013 9:58:18 AM UTC-5, Michael Bayer wrote:
>
> correction, this seems to work, though will try to improve:
>
> class UUID_ARRAY(TypeDecorator):
> impl = ARRAY(UUID, dimensions=1)
>
> def bind_expression(self, bindvalue):
> if bindvalue.callable:
> val = bindvalue.callable()
> else:
> val = bindvalue.value
> if val is None:
> val = []
> elif not hasattr(val, '__iter__'):
> return bindvalue
> return array(
> cast(literal(str(uuid_val)), UUID())
> for uuid_val in val
> )
>
>
>
>
> On Jan 7, 2013, at 9:55 AM, Michael Bayer wrote:
>
> this is ticket http://www.sqlalchemy.org/trac/ticket/2648 and cannot be
> worked around at this time. If you're working with arrays of UUID I'd
> recommend using psycopg2 type processors, as the previous poster has had
> success with.
>
>
>
> On Jan 7, 2013, at 9:39 AM, Hans Lellelid wrote:
>
> I am looking to adapt this code for a related array/type issue. The code
> from https://gist.github.com/4433940 works just fine for me (as expected)
> when building/executing the stmt directly, but not when using the ORM.
>
> When row is created using ORM, like this:
>
> # <snip>
> s = Session(bind=engine)
> e = Example()
> e.timestamp=datetime.datetime.utcnow(),
> e.num=2,
> e.guids = [uuid.uuid4(), uuid.uuid4()]
> s.add(e)
> s.commit()
>
> I get an error like this:
>
> <snip>
> return getter(visitor)(self, **kw)
> File
> "/home/hans/workspace/providence/env/lib/python2.7/site-packages/SQLAlchemy-0.8.0b2-py2.7-linux-x86_64.egg/sqlalchemy/sql/compiler.py",
>
> line 760, in visit_bindparam
> bind_expression = bindparam.type.bind_expression(bindparam)
> File "test_array.py", line 38, in bind_expression
> for uuid_val in val
> TypeError: 'object' object is not iterable
>
> (I can dump in full stack if that would be helpful.)
>
> Indeed, inspecting that reveals that it is simply an object() instance.
> I'm not sure where that is being set or whether there is an obvious
> workaround here. I'm sure I'm simply missing obvious when it comes to
> dealing with native array types and ORM entity instances.
>
> Thanks,
> Hans
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sqlalchemy/-/ZwxFp2iasq0J.
> To post to this group, send email to [email protected]<javascript:>
> .
> To unsubscribe from this group, send email to
> [email protected] <javascript:>.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected]<javascript:>
> .
> To unsubscribe from this group, send email to
> [email protected] <javascript:>.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sqlalchemy/-/XoOod2x83oUJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.