GORM : I inserted Data to Mysql DB but inserted empty value and null with golang gorm

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

GORM : I inserted Data to Mysql DB but inserted empty value and null with golang gorm

问题

我尝试使用Golang和GORM MySQL驱动程序插入数据,但插入的值为空。

var ipsin = ips{value: ipstr, org: org, country: country, city: city, latitude: latitude, longitude: longitude, isp: isp, asn: asn}

fmt.Println(ipsin)
// result := db.Select("value", "org", "country", "city", "latitude", "longitude", "isp", "asn").Create(&ipsin)
result := db.Table("ips").Create(&ipsin)

这是我的代码,请帮助我。

英文:

i try insert data with golang and gorm mysql driver but inserted value is null

var ipsin = ips{value: ipstr, org: org, country: country, city: city, latitude: latitude, longitude: longitude, isp: isp, asn: asn}

fmt.Println(ipsin)
//	result := db.Select("value", "org", "country", "city", "latitude", "longitude", "isp", "asn").Create(&ipsin)
result := db.Table("ips").Create(&ipsin)

thats my code
please help me

答案1

得分: 1

我认为所有的数据类型都是字符串,并设置了主键等,请根据您的要求进行更改。

您应该按照以下方式进行操作:

您的DTO应该像这样:

type Ips struct {
    CreatedAt    time.Time  `gorm:"autoCreateTime"` //可选
    UpdatedAt    time.Time  `gorm:"autoUpdateTime"` //可选
    Value        string     `json:"value" binding:"required"`
    Org          string     `json:"org" gorm:"primaryKey"`
    Country      string     `json:"country"`
    City         string     `json:"city"`
    Latitude     string     `json:"latitude"`
    Longitude    string     `json:"longitude"`
    Isp          string     `json:"isp"`
    Asn          string     `json:"asn"`
}

在数据库中创建或更改您的表格:

err := Client.AutoMigrate(&Ips{})

将您的数据插入表格中:

result := Client.Create(&Ips{Value: ipstr, Org: org, Country: country, 
                            City: city, Latitude: latitude,        
                            Longitude: longitude, Isp: isp, Asn: asn})
英文:

I think all data types are string and set mental primary key and etc. pls change it with your requirement.

you should do like below:

> your DTO like it

type Ips struct {
     CreatedAt    time.Time  `gorm:"autoCreateTime"` //optional
     UpdatedAt    time.Time  `gorm:"autoUpdateTime"` //optional
     Value        string     `json:"value" binding:"required"`
     Org          string     `json:"org" gorm:"primaryKey"`
     Country      string     `json:"country"`
     City         string     `json:"city"`
     Latitude     string     `json:"latitude"`
     Longitude    string     `json:"longitude"`
     Isp          string     `json:"isp"`
     Asn          string     `json:"asn"`
}

> create or change your table in DB

err := Client.AutoMigrate(&Ips{})

> insert your data into table

result := Client.Create(&Ips{Value: ipstr, Org: org, Country: country, 
                        City: city, Latitude: latitude,        
                        Longitude: longitude, Isp: isp, Asn: asn})

huangapple
  • 本文由 发表于 2022年10月18日 16:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/74107930.html
匿名

发表评论

匿名网友

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

确定