Since GORM v2 has switched to pgx, which has inet/cidr support.
If yes, what's the proper way to use it?
Tried something like this with no luck.
type Host struct {
IP *pgtype.Inet
IP2 net.IP
}
And got error
failed to guess net.IPNet's relations with github.com/jackc/pgtype.Inet'
And IP2 is recognized as byte array, not inet.
Comment From: github-actions[bot]
This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs.
Comment From: jinzhu
type Host struct {
IP pgtype.Inet
Comment From: roylee17
go type Host struct { IP pgtype.Inet
Same error. Am I missing something?
[error] failed to guess net.IPNet's relations with github.com/jackc/pgtype.Inet's field IPNet 1 g false
Adding custom datatypes works though.
https://github.com/go-gorm/datatypes/pull/5
Comment From: jinzhu
[error] failed to guess net.IPNet's relations with github.com/jackc/pgtype.Inet's field IPNet 1 g false
Maybe you need to set its db type with tag type
Comment From: roylee17
[error] failed to guess net.IPNet's relations with github.com/jackc/pgtype.Inet's field IPNet 1 g false
Maybe you need to set its db type with tag
type
type Host struct {
IP1 pgtype.Inet // failed to guess net.IPNet's relations with github.com/jackc/pgtype.Inet's field IPNet 1 g false
IP2 pgtype.Inet `gorm:"type:inet"` // sql: converting argument $1 type: cannot encode status undefined
ip3 net.IP // ERROR: column "ip3" is of type inet but expression is of type record (SQLSTATE 42804)
ip4 net.IP `gorm:"type:inet"` // ...
Tried a few combination, and none of them works so far. Automigration works as the table created does use "inet" as datatype. When it comes to insertion, however, each combination fails with different kind of error.
Comment From: atz
Simple style
type Mine struct {
IP pgtype.Inet
MAC pgtype.Macaddr
}
Automigration results in:
CREATE TABLE "mines" ("ip" bytea,"mac" bytea);
Inserts succeed, but that doesn't make use of the specialized types in PG.
Explicit type
IP pgtype.Inet `gorm:"type:inet"`
MAC pgtype.Macaddr `gorm:"type:macaddr"`
migrates with:
CREATE TABLE "mines" ("ip" inet,"mac" macaddr);
That does use the correct types, but inserts fail with ERROR: incorrect binary data format in bind parameter 2 (SQLSTATE 22P03), from:
INSERT INTO "mines" ("ip","mac") VALUES ('192.0.2.1/32','02:00:5e:10:00:00:00:01')
This is with Postgres 12 underneath. I think the error is because the MAC value is permitted by the pgtype (and net.HardwareAddr), but not by Postgres.
NOTE: The value '00:0a:95:9d:68:16' succeeds! So it is probably on pgtype to do better value checking or manipulation if it wants to provide better guarantees about postgres accepting the value.
Comment From: encryptblockr
@atz
for gorm:"not null", what do we put as default value for pgtype.Inet and pgtype.Macaddr?
Comment From: encryptblockr
@jinzhu @atz @roylee17
here is my model
import (
"github.com/jackc/pgtype"
)
...
ClientIp pgtype.Inet `json:"client_ip" gorm:"type:inet;not null"`
...
I am trying to parse the ip address which is in string format to type pgtype.Inet in postgresql database
like this when inserting into the database
import (
"net"
)
...
ClientIp: net.ParseIP(c.IP()),
...
we are told to parse the ip using net package but this is error from that
cannot use net.ParseIP(c.IP()) (type net.IP) as type pgtype.Inet in field value
I have also tried using net package for the model
import (
"net"
)
...
ClientIp net.IP `json:"client_ip" gorm:"type:inet;not null"`
...
but kept getting this error
sql: Scan error on column index 16, name "client_ip": unsupported Scan, storing driver.Value type string into type *net.IP
so how do we store inet values inside postgresql using GORM?
https://github.com/jackc/pgtype/issues/180
Comment From: encryptblockr
@jinzhu
[error] failed to guess net.IPNet's relations with github.com/jackc/pgtype.Inet's field IPNet 1 g false
Maybe you need to set its db type with tag
type
haven't you stored ip in postgresql ever before? why not just share code that works?