PostgreSQLのinet型とはなんなのか
今作ってるサービスをPostgreSQLからMySQLに移行したときに、migrateファイルで「inetなんてメソッドないよ!」って言われてしまった。
inet...?って思ってmigrationファイルを見てみると、どうやら型の名前みたいだ。PostgreSQL専用の型なのかな?と思って調べたのでメモ。
Rails GuidesのEdge版によると、
The types inet and cidr are mapped to Ruby IPAddr objects. The macaddr type is mapped to normal text.
via: http://edgeguides.rubyonrails.org/active_record_postgresql.html#network-address-types
そして、PostgreSQLのサイトがこちら
http://www.postgresql.org/docs/9.1/static/datatype-net-types.html
PostgreSQL offers data types to store IPv4, IPv6, and MAC addresses, as shown in Table 8-21. It is better to use these types instead of plain text types to store network addresses, because these types offer input error checking and specialized operators and functions
ということで、IPアドレスを保持するならstring系の型よりこちらの方が推奨されていて、inetに関する記述部分をそのまま抜き出すと、
8.9.1. inet
The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. The subnet is represented by the number of network address bits present in the host address (the "netmask"). If the netmask is 32 and the address is IPv4, then the value does not indicate a subnet, only a single host. In IPv6, the address length is 128 bits, so 128 bits specify a unique host address. Note that if you want to accept only networks, you should use the cidr type rather than inet.
The input format for this type is address/y where address is an IPv4 or IPv6 address and y is the number of bits in the netmask. If the /y portion is missing, the netmask is 32 for IPv4 and 128 for IPv6, so the value represents just a single host. On display, the /y portion is suppressed if the netmask specifies a single host.
となっている。
大事なのは、
ということっぽい。