Gorm在保存/创建时对结构进行序列化。

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

Gorm serialize struct on save/create

问题

我有一个结构体,它是一个表的模型。一切都很好,直到我想将类型结构体添加到其中并将其序列化为JSON(Location)。

type Dome struct {
	gorm.Model
	Location Location `json:"location" gorm:"serializer:json"`
	Title    string   `json:"title" gorm:"type:varchar(100)"`
}

type Location struct {
	X1 int
	Y1 int
	X2 int
	Y2 int
}

当使用.Updates()方法时,这些值被序列化并保存到列中。但是当使用Create或Save方法时,会抛出错误:

sql: converting argument $ type: unsupported type Location, a struct

据我所了解,Gorm已经有一些默认的序列化器,比如json。它似乎在Updates方法上起作用,但在任何Create方法上都不起作用。而且在调试时,我看到这些值被反序列化并再次出现在结构体中。

我找不到答案,我可能漏掉了什么,也许需要添加其他内容,但我经验不够丰富。如何使用Gorm将结构体序列化到列中?

英文:

I have struct, that is model for table. All was good, until I want to add type struct into it and serialize it to json (Location).

type Dome struct {
	gorm.Model
	Location Location `json:"location" gorm:"serializer:json"`
	Title    string   `json:"title" gorm:"type:varchar(100)"`
}

type Location struct {
	X1 int
	Y1 int
	X2 int
	Y2 int
}

When doing .Updates(), those values are serialized and saved into column. But when doing Create or Save, it throw error

sql: converting argument $ type: unsupported type Location, a struct

From what I understood, Gorm already have some default serializers, like json. And it seems to work on Updates, but not on any Create. Also when debugging I see those values deserialized and again in struct.

I can't find answer what am I missing, maybe need to add something else, but I am not that experienced. How to do that serialization into column from struct using Gorm?

答案1

得分: 1

package main

import (
	"fmt"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type Dome struct {
	gorm.Model
	Location Location `json:"location" gorm:"serializer:json"`
	Title    string   `json:"title" gorm:"type:varchar(100)"`
}

type Location struct {
	X1 int
	Y1 int
	X2 int
	Y2 int
}

func main() {
	db, _ := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})

	/*err := db.AutoMigrate(&Dome{})
	if err != nil {
		return
	}*/

	l := Location{}
	l.Y1 = 1
	l.X1 = 2
	l.X2 = 3
	l.Y2 = 4

	d := Dome{}
	d.Title = "test"
	d.Location = l
	db.Create(&d)

	d.Location.Y2 = 6
	db.Save(&d)

	d.Location.X2 = 4
	db.Updates(&d)

	_target := []*Dome{}
	db.Find(&_target)
	for _, t := range _target {
		fmt.Printf("%+v \n", t)
	}
}

我尝试了这种方式,serializer:json 没有问题。

输出结果:

&{Model:{ID:1 CreatedAt:2022-08-06 14:39:59.184012808 +0530 +0530 UpdatedAt:2022-08-06 14:39:59.184012808 +0530 +0530 DeletedAt:{Time:0001-01-01 00:00:00 +0000 UTC Valid:false}} Location:{X1:2 Y1:1 X2:3 Y2:4} Title:test} 
&{Model:{ID:2 CreatedAt:2022-08-06 14:40:55.666162544 +0530 +0530 UpdatedAt:2022-08-06 14:40:55.677998201 +0530 +0530 DeletedAt:{Time:0001-01-01 00:00:00 +0000 UTC Valid:false}} Location:{X1:2 Y1:1 X2:3 Y2:6} Title:test} 
&{Model:{ID:3 CreatedAt:2022-08-06 14:41:29.361814733 +0530 +0530 UpdatedAt:2022-08-06 14:41:29.367237119 +0530 +0530 DeletedAt:{Time:0001-01-01 00:00:00 +0000 UTC Valid:false}} Location:{X1:2 Y1:1 X2:4 Y2:6} Title:test} 

根据文档,你可以注册序列化器并实现如何序列化和反序列化数据。
https://gorm.io/docs/serializer.html#Register-Serializer

英文:
package main

import (
	"fmt"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type Dome struct {
	gorm.Model
	Location Location `json:"location" gorm:"serializer:json"`
	Title    string   `json:"title" gorm:"type:varchar(100)"`
}

type Location struct {
	X1 int
	Y1 int
	X2 int
	Y2 int
}

func main() {
	db, _ := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})

	/*err := db.AutoMigrate(&Dome{})
	if err != nil {
		return
	}*/

		l := Location{}
		l.Y1 = 1
		l.X1 = 2
		l.X2 = 3
		l.Y2 = 4

		d := Dome{}
		d.Title = "test"
		d.Location = l
		db.Create(&d)

		d.Location.Y2 = 6
		db.Save(&d)

		d.Location.X2 = 4
		db.Updates(&d)

	_target := []*Dome{}
	db.Find(&_target)
	for _, t := range _target {
		fmt.Printf("%+v \n", t)
	}
}

I tried like this way and serializer:json worked without an issue.

output:

&{Model:{ID:1 CreatedAt:2022-08-06 14:39:59.184012808 +0530 +0530 UpdatedAt:2022-08-06 14:39:59.184012808 +0530 +0530 DeletedAt:{Time:0001-01-01 00:00:00 +0000 UTC Valid:false}} Location:{X1:2 Y1:1 X2:3 Y2:4} Title:test} 
&{Model:{ID:2 CreatedAt:2022-08-06 14:40:55.666162544 +0530 +0530 UpdatedAt:2022-08-06 14:40:55.677998201 +0530 +0530 DeletedAt:{Time:0001-01-01 00:00:00 +0000 UTC Valid:false}} Location:{X1:2 Y1:1 X2:3 Y2:6} Title:test} 
&{Model:{ID:3 CreatedAt:2022-08-06 14:41:29.361814733 +0530 +0530 UpdatedAt:2022-08-06 14:41:29.367237119 +0530 +0530 DeletedAt:{Time:0001-01-01 00:00:00 +0000 UTC Valid:false}} Location:{X1:2 Y1:1 X2:4 Y2:6} Title:test} 

Gorm在保存/创建时对结构进行序列化。

According to documentation you can register serializer and implement how to serialize and deserialize data.
https://gorm.io/docs/serializer.html#Register-Serializer

huangapple
  • 本文由 发表于 2022年8月6日 16:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73258344.html
匿名

发表评论

匿名网友

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

确定