GORM Playground Link
https://github.com/go-gorm/playground/pull/507
code
package main
import (
"log"
"os"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
type Product struct {
gorm.Model
Code string
Price uint
Author Author `gorm:"type:JSON;serializer:json"`
}
type Author struct {
Name string
Email string
}
func main() {
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // Log level
Colorful: false, // 禁用彩色打印
},
)
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
NoLowerCase: true,
},
Logger: newLogger,
})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Product{})
p1 := &Product{Code: "D1", Price: 100, Author: Author{
Name: "chen",
}}
if err = db.Create(p1).Error; err != nil { // is ok
log.Println(err)
}
p2 := &Product{Code: "D2", Price: 100}
if err = db.Create(p2).Error; err != nil { // is failed
log.Println(err)
}
}
Description
env: - go1.19 - gorm.io/driver/sqlite v1.3.6 - gorm.io/gorm v1.23.8
I found insert non-zero value is ok, zero value will failed.
log:
# go build --tags "sqlite_json1" github.com/mattn/go-sqlite3
# go build
# ./t
2022/08/19 10:36:29 /opt/mark/go/pkg/mod/gorm.io/driver/sqlite@v1.3.6/migrator.go:33
[0.107ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="Product"
2022/08/19 10:36:29 /opt/mark/test/gorm/g.go:47
[18.609ms] [rows:0] CREATE TABLE `Product` (`ID` integer,`CreatedAt` datetime,`UpdatedAt` datetime,`DeletedAt` datetime,`Code` text,`Price` integer,`Author` JSON,PRIMARY KEY (`ID`))
2022/08/19 10:36:29 /opt/mark/go/pkg/mod/gorm.io/driver/sqlite@v1.3.6/migrator.go:293
[10.302ms] [rows:0] CREATE INDEX `idx_Product_DeletedAt` ON `Product`(`DeletedAt`)
2022/08/19 10:36:29 /opt/mark/test/gorm/g.go:52
[10.265ms] [rows:1] INSERT INTO `Product` (`CreatedAt`,`UpdatedAt`,`DeletedAt`,`Code`,`Price`,`Author`) VALUES ("2022-08-19 10:36:29.928","2022-08-19 10:36:29.928",NULL,"D1",100,"{\"Name\":\"chen\",\"Email\":\"\"}") RETURNING `ID`
2022/08/19 10:36:29 /opt/mark/test/gorm/g.go:57 sql: converting argument $6 type: unsupported type main.Author, a struct
[0.242ms] [rows:0] INSERT INTO `Product` (`CreatedAt`,`UpdatedAt`,`DeletedAt`,`Code`,`Price`,`Author`) VALUES ("2022-08-19 10:36:29.938","2022-08-19 10:36:29.938",NULL,"D2",100,"{ }") RETURNING `ID`
2022/08/19 10:36:29 sql: converting argument $6 type: unsupported type main.Author, a struct
# sqlite3 test.db
SQLite version 3.37.2 2022-01-06 13:25:41
Enter ".help" for usage hints.
sqlite> select * from Product;
1|2022-08-19 10:36:29.928648172+08:00|2022-08-19 10:36:29.928648172+08:00||D1|100|{"Name":"chen","Email":""}
sqlite> INSERT INTO `Product` (`CreatedAt`,`UpdatedAt`,`DeletedAt`,`Code`,`Price`,`Author`) VALUES ("2022-08-19 10:36:29.938","2022-08-19 10:36:29.938",NULL,"D2",100,"{ }") RETURNING `ID`;
2
sqlite> select * from Product;
1|2022-08-19 10:36:29.928648172+08:00|2022-08-19 10:36:29.928648172+08:00||D1|100|{"Name":"chen","Email":""}
2|2022-08-19 10:36:29.938|2022-08-19 10:36:29.938||D2|100|{ }
sqlite> .exit
Comment From: meilihao
It is the same on mysql(10.6.7-MariaDB-2ubuntu1.1 Ubuntu 22.04).
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: a631807682
duplicated of https://github.com/go-gorm/gorm/issues/5524