Re: INET type and arrays: too much casting?

Stephane Bortzmeyer <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Mon, Sep 15, 2008 at 08:05:15AM -0700,
 Chris Cogdon <[email protected]> wrote 
 a message of 64 lines which said:

> The above python statement produces this command to the back-end:
>
> insert into foobar values ( array[E'2001::1',E'2001:db8::dead:babe'] )
>
> Typing that command into psql directly produces the same error. 

Yes, the proper syntax, the one psycopg should have generated, is:

essais=> insert into foobar values ('{2001::1 , 2001:db8::dead:babe}') ;
INSERT 0 1

(Quoting the array but not the array members.)

> So, it's postgresql that has a problem turning a text[] into a
> inet[], not psycopg.

I disagree. psycopg should not have quoted the addresses. I assume it
did so because, Python being weakly typed, psycopg cannot know that
2001::1 is a PostgreSQL special type (Python knows about booleans,
about integers - I have no problems filling arrays of integers - but
not UUID or IP addresses).

I do not know if there is a workaround for that?

> However, the following, including an explicit cast, works just fine
>
> c.execute ( 'insert into foobar values (%s::inet[])', [["2001::1",  
> "2001:db8::dead:babe"]])

Yes, but this would force me to change a lot of things in my
application (the INSERT is at a point where the type is no longer
known - it should be sent together with the value).

For integers and booleans, arrays work automagically.

For my IP addresses, should I use adapters?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.