Question
Hello, I just want to save data from golang using gorm to my mysql database, In this case, this is my struct:
type UserData struct {
Id
Description json.RawMessage `json:"description"`
}
For this struct I'm trying to save a json like an interface{} because could grow the information inside, and this information it's coming from frontend.
I execute the migration and create a "longblob" field (this it's ok) but when I send this information to the bbdd:
INSERT INTO `user_data` (`description`) VALUES ((123,34,101,110,34,58,34,65,100,109,105,110,105,115,116,114,97,116,111,114,34,44,34,101,115,34,58,34,65,100,109,105,110,105,115,116,114,97,100,111,114,34,125))
I got the following reply:
Error 1241: Operand should contain 1 column(s)
The document you expected this should be explained
Expected answer
If I add to the insert some "" before and after the insert works, but I don't know if It will work with the json.Marshal at this moment.
How can i fix this situation? Is there any another solution to my problem? Another way to save json strings in bbdd?
Thank you,
Comment From: ghost
hello, you can try to see https://gorm.cn/docs/data_types.html ~
Comment From: juancer
hello, you can try to see https://gorm.cn/docs/data_types.html ~
Hi, yes, I already read it, but, I'm not sure about that's the solution for my case.
I don't know if the problem is when gorm add () instead of "" to the byte string.
Thank you for your help @longlihale
Comment From: ghost
because description is byte slice and it maybe cannot save directly to MySQL, so you need to customize a type instead of json.RawMessage and achieving Valuer interface.
Comment From: juancer
Hi, so, I should save the string of the json in the bbdd?
Comment From: ghost
Hi, so, I should save the string of the json in the bbdd?
I think so
Comment From: juancer
then, in that case, I don't understand the relation between json.rawMessage and gorm, because, in that case, I'll return an string with my API, or make a bigger process to convert that information to a valid json. I don't think about that's the only solution
Comment From: juancer
the main problem with that solution @longlihale is when I preload the tables for the relations, I got the json in string format like this:
"description": "{\"en\":\"Website\",\"es\":\"Página web\"}"
If I use json.RawMessage, I got the correct format of the json
Comment From: ghost
the main problem with that solution @longlihale is when I preload the tables for the relations, I got the json in string format like this:
"description": "{\"en\":\"Website\",\"es\":\"Página web\"}"If I use json.RawMessage, I got the correct format of the json
yes, but use json.RawMessage should to achieving Valuer interface, otherwise generate insert sql is wrong.
aha, why not use string type and use extra json.Unmarshal to sovle it.
Comment From: juancer
could you please provide me an example? Thanks,
Comment From: ghost
could you please provide me an example? Thanks, OK
Comment From: liweitingwt
could you please provide me an example? Thanks,
you can find a demo in the pr (https://github.com/go-gorm/playground/pull/424) for this issue, hope it can resolve your confuse.
Comment From: juancer
I have this situation:
type User struct {
gorm.Model
Name string
Age uint
Birthday *time.Time
Languages []Language `gorm:"many2many:UserSpeak"`
Active bool
}
type Language struct {
gorm.Model
Code string `gorm:"primarykey"`
Name json.RawMessage
}
When I'm trying to create the Get i got the problem.
func GetUser(db *gorm.DB, name string) (User, error) {
var user User
result := db.
Preload("Language").
Where("Name=?", name).Find(&user)
return user, result.Error
}
The solution provided here could help, but, when you create the preload, it's when I found the problem.
Sorry about the misunderstanding on my first example and thank you for all the help.
Regards,
Comment From: juancer
finally I got the solution... I create two fields with the name
type User struct {
gorm.Model
DescriptionInt string `json:"descriptionInt,omitempty"`
Description Description `gorm:"-" json:"description"`
}
type Description struct {
EN string `json:"en,omitempty"`
ES string `json:"es,omitempty"`
FR string `json:"fr,omitempty"`
PL string `json:"pl,omitempty"`
RU string `json:"ru,omitempty"`
CN string `json:"cn,omitempty"`
}
And after in the code I made this change:
//Create a variable for the description
var desc entities.Description
//unmarshall
bytes = []byte(user.DescriptionInt)
err = json.Unmarshal(bytes, &desc)
if err != nil {
panic(err)
}
user.Description = desc
user.DescriptionInt = ""
And I got the correct response. It's a go situation and not a gorm situation.
Thanks everyone for the help.
Regards,
Comment From: bmikaili
Seems to me like a problem in handling JSON in GORM, no? I think this should be reopened
Comment From: juancer
Seems to me like a problem in handling JSON in GORM, no? I think this should be reopened
Hi, I'm not working that project any more, then, I'm not sure about the solution on that moment. I you need it, reopen it, or create a new one with your examples and refer to this one. Best,