MySQL 列属性为 JSON 时,若此列未设值,则跳过插入,避免引起错误:Invalid JSON text: "The document is empty." at position 0 in value

This commit is contained in:
donghao 2022-03-27 18:38:42 +08:00
parent 3180c418c2
commit 7ec9d5c0a2
2 changed files with 5 additions and 0 deletions

View File

@ -304,6 +304,7 @@ func (db *mysql) SQLType(c *schemas.Column) string {
c.Length = 40
case schemas.Json:
res = schemas.Text
c.IsJSON = true
case schemas.UnsignedInt:
res = schemas.Int
isUnsigned = true

View File

@ -501,6 +501,10 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
continue
}
if col.IsJSON && fieldValue.String() == "" {
continue
}
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
if _, ok := getFlagForColumn(session.statement.NullableMap, col); ok {
if col.Nullable && utils.IsValueZero(fieldValue) {