Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wierd data in inet data type #37

Closed
hh-h opened this issue Oct 30, 2016 · 4 comments · Fixed by #203
Closed

Wierd data in inet data type #37

hh-h opened this issue Oct 30, 2016 · 4 comments · Fixed by #203
Labels

Comments

@hh-h
Copy link

hh-h commented Oct 30, 2016

Hello! Thank you for your awesome job!

This code creates different values in table

await conn.execute("CREATE table test1 (id serial primary key, ip inet not null)")
ip = '10.78.243.196'
ip32 = '10.78.243.196/32'
await conn.execute("INSERT INTO test1 (ip) VALUES($1)", ip)
await conn.execute("INSERT INTO test1 (ip) VALUES($1)", '10.78.243.196')
await conn.execute("INSERT INTO test1 (ip) VALUES('10.78.243.196')")
await conn.execute("INSERT INTO test1 (ip) VALUES('10.78.243.196/32')")
await conn.execute("INSERT INTO test1 (ip) VALUES($1)", ip32) // Exception ValueError: '10.78.243.196/32' does not appear to be an IPv4 or IPv6 address
 id |       ip
----+-----------------
  1 | 10.78.243.196/0
  2 | 10.78.243.196/0
  3 | 10.78.243.196
  4 | 10.78.243.196
(3 rows)

I have latest version in pip asyncpg==0.6.3

                                         version
------------------------------------------------------------------------------------------
 PostgreSQL 9.5.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit

Could you please take a look?

@elprans
Copy link
Member

elprans commented Oct 31, 2016

Yea, ayncpg is currently (incorrectly) assumes that all inet columns are host addresses, but Postgres, in fact, allows network addresses there too. If you need to store network addresses, use the cidr type.

@elprans elprans added the bug label Oct 31, 2016
@hh-h
Copy link
Author

hh-h commented Nov 14, 2016

Awesome, thank you!

@hatarist
Copy link

hatarist commented Oct 2, 2017

Sorry for posting in the existing issue, but the asyncpg 0.12.0 is still having a little bit of that problem.

The behaviour is still weird. The column has inet type:

CREATE TABLE test (ip inet, source text);

Example queries:

await pg.execute('INSERT INTO test (ip, source) VALUES ($1, $2);', '10.11.12.13', 'testip-string')
await pg.execute('INSERT INTO test (ip, source) VALUES ($1, $2);', ip_address('10.11.12.13'), 'testip-ipv4')
await pg.execute('INSERT INTO test (ip, source) VALUES ($1, $2);', ip_network('10.11.12.13'), 'testip-ipv4-net')

Which are, then, stored in the database like that:

=> SELECT ip, source FROM test;
    ip       |     source
----------------+-----------------
10.11.12.13/0 | testip-string
10.11.12.13/0 | testip-ipv4
10.11.12.13   | testip-ipv4-net

The strangest thing is that IPv4Network being applied to the single IP address actually sends it as I intended to send it, but as an IPv4Address.

Could anybody re-open the issue, please? I hope I'm not wrong here.

elprans added a commit that referenced this issue Oct 2, 2017
The inet codec is confusing the network prefix length with the network
mask length, which causes incorrect wire encoding.  This was masked by a
symmetrical bug in the decoder.

Fixes #37.
@elprans elprans reopened this Oct 2, 2017
@hatarist
Copy link

hatarist commented Oct 2, 2017

Wow, thanks a lot! I confirm that it's sorted out, at least for the IPv4Address :)

elprans added a commit that referenced this issue Oct 10, 2017
The inet codec is confusing the network prefix length with the network
mask length, which causes incorrect wire encoding.  This was masked by a
symmetrical bug in the decoder.

Fixes #37.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants