gf icon indicating copy to clipboard operation
gf copied to clipboard

2.6.3 nil and empty arrays should not be ignored when inserting and updating JSON fields

Open macnie opened this issue 1 year ago • 7 comments

数据表有JSON字段类型时,保存数据,数据为空数组时,2.6版本会忽略此字段。但MYSQL中,JSON字段是必须赋值的。就造成很大困扰。

macnie avatar Feb 19 '24 04:02 macnie

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


When the data table has a JSON field type, save the data. When the data is an empty array, version 2.6 will ignore this field. But in MYSQL, JSON fields must be assigned values. It caused a lot of trouble.

Issues-translate-bot avatar Feb 19 '24 04:02 Issues-translate-bot

这些BUG什么时候能修复呢

macnie avatar Feb 21 '24 06:02 macnie

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


When will these bugs be fixed?

Issues-translate-bot avatar Feb 21 '24 06:02 Issues-translate-bot

比如:

dao.User.Ctx(ctx).Insert(do.User{
   Id:1,
   Name:"小李",
   Files:[]string{},
})

这时,生成的SQL语句为:insert into user (id,name) values(1,"小李") 没有files字段。但files字段是JSON字段,必须赋值。所以SQL报错。

macnie avatar Mar 05 '24 13:03 macnie

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


For example: dao.User.Ctx(ctx).Insert(do.User{ Id:1, Name:"Xiao Li", Files:[]string{}, })

At this time, the generated SQL statement is: insert into user (id, name) values ​​(1, "Xiao Li") without the files field. But the files field is a JSON field and must be assigned a value. So SQL reports an error.

Issues-translate-bot avatar Mar 05 '24 13:03 Issues-translate-bot

这个地方很诡异, 如何判断这个json是[]{} ,代码如何赋值呢? 根据结构体匹配字段的类型,再赋值默认值吗? 是不是可以在数据库字段设置默认值处理

houseme avatar Apr 02 '24 15:04 houseme

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This place is very weird, How to determine whether this json is [] or {}, and how to assign the value in the code? Should a default value be assigned based on the type of the field matched in the structure? Is it possible to set default value processing in database fields?

Issues-translate-bot avatar Apr 02 '24 15:04 Issues-translate-bot