#11442: Postgresql backend casts inet types to text, breaks IP operations and 
IPv6
lookups.
------------------------------------------+---------------------------------
 Reporter:  eide                          |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  1.0       
 Keywords:  ipv6 postgres inet            |       Stage:  Unreviewed
Has_patch:  0                             |  
------------------------------------------+---------------------------------
 Ticket #708 describes a problem with LIKE operations on inet types in
 postgresql. The solution was to cast inet to text using the HOST()
 function.

 But by casting inet to text none of the network operations in postgresql
 will work, and IPv6 lookups are pretty much broken. In the database I'm
 currently using, doing a HOST() on a IPv6 address will always produce a
 compressed URL. So if I'm checking against a fullsize address in django
 the lookup will fail, even though they are the same address.

 Here's an example of what I'm talking about:
 {{{
 my_db=# CREATE TABLE my_ips (ip inet);
 CREATE TABLE
                             ^
 my_db=# INSERT INTO my_ips VALUES
 ('2001:0db8:0000:0000:0000:0000:0000:0001');
 INSERT 0 1

 my_db=# SELECT * FROM my_ips WHERE ip =
 '2001:0db8:0000:0000:0000:0000:0000:0001';
      ip
 -------------
  2001:db8::1
 (1 row)

 my_db=# SELECT * FROM my_ips WHERE ip = '2001:db8::1';
      ip
 -------------
  2001:db8::1
 (1 row)
 }}}
 So far so good, but when you throw HOST() into the picture, this happens:
 {{{
 my_db=# SELECT * FROM my_ips WHERE HOST(ip) = '2001:db8::1';
      ip
 -------------
  2001:db8::1
 (1 row)

 my_db=# SELECT * FROM my_ips WHERE HOST(ip) =
 '2001:0db8:0000:0000:0000:0000:0000:0001';
  ip
 ----
 (0 rows)
 }}}
 2001:db8::1 and 2001:0db8:0000:0000:0000:0000:0000:0001 are the same
 address, just displayed on different forms.

 Currently I always make sure that I pass a compressed IP to the models
 with IPAddressFields. That does however assume that all postgresql
 databases will always return IPv6 addresses on the compressed form, and I
 do not know if that's correct.

 The correct solution would be to not cast inet to text.

 Also, the postgresql documentation on
 [http://www.postgresql.org/docs/8.2/static/functions-net.html Network
 Address Functions and Operators] states that:
 {{{
 The host, text, and abbrev functions are primarily intended to offer
 alternative display formats.
 }}}
 So using HOST() for lookups is acctually kind of wrong in the first place.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11442>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
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/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to