Skip to content

Commit

Permalink
feat: add a warn about SQLite default tag (#732)
Browse files Browse the repository at this point in the history
* feat: add a warn about SQLite default tag

* fix error code

* fix error code
  • Loading branch information
black-06 authored Jan 29, 2024
1 parent 82cd0a4 commit 5633250
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pages/docs/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

### <span id="upsert">Upsert / On Conflict</span>
Expand Down

0 comments on commit 5633250

Please sign in to comment.