From 563325033e07b492d791cea320cfe44434e4bc2d Mon Sep 17 00:00:00 2001 From: black-06 Date: Mon, 29 Jan 2024 11:41:37 +0800 Subject: [PATCH] feat: add a warn about SQLite default tag (#732) * feat: add a warn about SQLite default tag * fix error code * fix error code --- pages/docs/create.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pages/docs/create.md b/pages/docs/create.md index 9b18d0930f9..6ee44ce0a68 100644 --- a/pages/docs/create.md +++ b/pages/docs/create.md @@ -257,6 +257,32 @@ type User struct { } ``` +{% note warn %} +**NOTE** **SQLite** doesn't support some records are default values when batch insert. +See [SQLite Insert stmt](https://www.sqlite.org/lang_insert.html). For example: + +```go +type Pet struct { + Name string `gorm:"default:cat"` +} + +// In SQLite, this is not supported, so GORM will build a wrong SQL to raise error: +// INSERT INTO `pets` (`name`) VALUES ("dog"),(DEFAULT) RETURNING `name` +db.Create(&[]Pet{{Name: "dog"}, {}}) +``` +A viable alternative is to assign default value to fields in the hook, e.g. + +```go +func (p *Pet) BeforeCreate(tx *gorm.DB) (err error) { + if p.Name == "" { + p.Name = "cat" + } +} +``` + +You can see more info in [issues#6335](https://github.com/go-gorm/gorm/issues/6335) +{% endnote %} + When using virtual/generated value, you might need to disable its creating/updating permission, check out [Field-Level Permission](models.html#field_permission) ### Upsert / On Conflict