How to store ipv4 and ipv6 addresses in postgresql using GORM

huangapple go评论96阅读模式
英文:

How to store ipv4 and ipv6 addresses in postgresql using GORM

问题

我们可以使用GORM和PostgreSQL来插入和选择具有inet数据类型的表中的IP地址。以下是您的模型示例:

import (
    "github.com/jackc/pgtype"
)

...
ClientIp     pgtype.Inet `json:"client_ip" gorm:"type:inet;not null"`
...

您可以尝试将字符串格式的IP地址解析为pgtype.Inet类型,然后将其插入到数据库中,如下所示:

import (
    "net"
)

...
ClientIp:     pgtype.Inet{IPNet: net.ParseIP(c.IP())},
...

在这里,我们使用net包中的ParseIP函数来解析IP地址。但是,您可能会遇到以下错误:

cannot use net.ParseIP(c.IP()) (type net.IP) as type pgtype.Inet in field value

您还尝试将net包用于模型,如下所示:

import (
    "net"
)

...
ClientIp     net.IP `json:"client_ip" gorm:"type:inet;not null"`
...

但是,您可能会遇到以下错误:

sql: Scan error on column index 16, name "client_ip": unsupported Scan, storing driver.Value type string into type *net.IP

所以,我们应该如何使用GORM将inet值存储到PostgreSQL中呢?

您可以尝试使用以下方法将inet值存储到PostgreSQL中:

import (
    "github.com/jackc/pgtype"
    "net"
)

...
var clientIP pgtype.Inet
clientIP.Set(net.ParseIP(c.IP()))
ClientIp: clientIP,
...

这样,您可以使用pgtype.Inet类型的变量来存储IP地址,并将其分配给ClientIp字段。

希望这可以帮助到您!

英文:

How do we insert and select from a table with inet datatype for ip addresses using GORM with postgresql?

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?

答案1

得分: 2

模型:

import (
    "github.com/jackc/pgtype"
)

...
ClientIp     pgtype.Inet `json:"client_ip" gorm:"type:inet;not null"`
...

然后你可以创建一个名为SetInet的函数,如下所示:

func SetInet(ip string) pgtype.Inet {
    var inet pgtype.Inet
    inet.Set(ip)
    return inet
}

然后你可以使用该函数来设置inet

...
ClientIp:     SetInet("127.0.0.1"),
...

如果你不想创建这样的函数,你可以从包含字段pgtype.Inet的变量中设置它

示例:

...
type Ip struct {
    gorm.Model
    ClientIp pgtype.Inet `gorm:"type:inet;not null"`
}
...
var ip Ip

ip.ClientIp.Set("127.0.0.1")
...
英文:

model :

import (
    "github.com/jackc/pgtype"
)

...
ClientIp     pgtype.Inet `json:"client_ip" gorm:"type:inet;not null"`
...

then you can create func SetInet like this

func SetInet(ip string) pgtype.Inet {
	var inet pgtype.Inet
	inet.Set(ip)
	return inet
}

then you can use that function to set inet :

...
ClientIp:     SetInet("127.0.0.1"),
...

If you don't want to create func like that you can set it from variable that contain field pgtype.Inet

example :

...
	type Ip struct {
		gorm.Model
		ClientIp pgtype.Inet `gorm:"type:inet;not null"`
	}
...
	var ip Ip

	ip.ClientIp.Set("127.0.0.1")
...

huangapple
  • 本文由 发表于 2022年8月5日 18:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/73248423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定