mirror of https://github.com/go-gorm/gorm.git
Compare commits
5 Commits
ed90d8d41f
...
ea798f4d8e
Author | SHA1 | Date |
---|---|---|
black-06 | ea798f4d8e | |
Jinzhu | 6bfccf8afa | |
black | 441092d82f | |
Jinzhu | 6a03c283a9 | |
black | 174cfc5cbd |
|
@ -3,7 +3,6 @@ package callbacks
|
|||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
@ -354,8 +353,9 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
|
|||
for _, column := range values.Columns {
|
||||
if field := stmt.Schema.LookUpField(column.Name); field != nil {
|
||||
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
|
||||
if !field.PrimaryKey && (!field.HasDefaultValue || field.DefaultValueInterface != nil ||
|
||||
strings.EqualFold(field.DefaultValue, "NULL")) && field.AutoCreateTime == 0 {
|
||||
// We can confirm the column either has a value or default value,
|
||||
// so checking HasDefaultValue again will cause some columns to be missed.
|
||||
if !field.PrimaryKey && field.AutoCreateTime == 0 {
|
||||
if field.AutoUpdateTime > 0 {
|
||||
assignment := clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: curTime}
|
||||
switch field.AutoUpdateTime {
|
||||
|
|
2
go.mod
2
go.mod
|
@ -5,5 +5,5 @@ go 1.18
|
|||
require (
|
||||
github.com/jinzhu/inflection v1.0.0
|
||||
github.com/jinzhu/now v1.1.5
|
||||
golang.org/x/text v0.14.0
|
||||
golang.org/x/text v0.20.0
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -2,5 +2,5 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
|
|||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
|
||||
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
||||
|
|
1
gorm.go
1
gorm.go
|
@ -183,7 +183,6 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
|
|||
|
||||
if config.Dialector != nil {
|
||||
err = config.Dialector.Initialize(db)
|
||||
|
||||
if err != nil {
|
||||
if db, _ := db.DB(); db != nil {
|
||||
_ = db.Close()
|
||||
|
|
|
@ -3,6 +3,7 @@ package tests_test
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/schema"
|
||||
|
@ -183,6 +184,56 @@ func TestForeignKeyConstraintsBelongsTo(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFullSaveAssociationsWithJSONDefault(t *testing.T) {
|
||||
if DB.Dialector.Name() == "mysql" {
|
||||
t.Skip() // mysql json can't have a default value
|
||||
}
|
||||
|
||||
type ValueDep struct {
|
||||
ID int
|
||||
ValueID int
|
||||
Name string
|
||||
Params datatypes.JSONMap `gorm:"default:'{}'"`
|
||||
}
|
||||
type Value struct {
|
||||
ID int
|
||||
Name string
|
||||
Dep ValueDep
|
||||
}
|
||||
|
||||
if err := DB.Migrator().DropTable(&ValueDep{}, &Value{}); err != nil {
|
||||
t.Fatalf("failed to drop value table, got err: %v", err)
|
||||
}
|
||||
if err := DB.AutoMigrate(&ValueDep{}, &Value{}); err != nil {
|
||||
t.Fatalf("failed to migrate value table, got err: %v", err)
|
||||
}
|
||||
|
||||
if err := DB.Create(&Value{
|
||||
Name: "foo",
|
||||
Dep: ValueDep{Name: "bar", Params: map[string]interface{}{"foo": "bar"}},
|
||||
}).Error; err != nil {
|
||||
t.Errorf("failed to create value, got err: %v", err)
|
||||
}
|
||||
|
||||
var value Value
|
||||
if err := DB.Preload("Dep").First(&value).Error; err != nil {
|
||||
t.Errorf("failed to find value, got err: %v", err)
|
||||
}
|
||||
|
||||
value.Dep.Params["foo"] = "new bar"
|
||||
if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&value).Error; err != nil {
|
||||
t.Errorf("failed to svae value, got err: %v", err)
|
||||
}
|
||||
|
||||
var result Value
|
||||
if err := DB.Preload("Dep").First(&result).Error; err != nil {
|
||||
t.Errorf("failed to find value, got err: %v", err)
|
||||
}
|
||||
if result.Dep.Params["foo"] != "new bar" {
|
||||
t.Errorf("failed to save value dep params, got: %v", result.Dep.Params)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFullSaveAssociations(t *testing.T) {
|
||||
coupon := &Coupon{
|
||||
AppliesToProduct: []*CouponProduct{
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/jinzhu/now"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
. "gorm.io/gorm/utils/tests"
|
||||
|
@ -617,6 +618,38 @@ func TestCreateOnConflictWithDefaultNull(t *testing.T) {
|
|||
AssertEqual(t, u2.Mobile, "133xxxx")
|
||||
}
|
||||
|
||||
func TestCreateOnConflictWithDefaultJSON(t *testing.T) {
|
||||
if DB.Dialector.Name() == "mysql" {
|
||||
t.Skip() // mysql json can't have a default value
|
||||
}
|
||||
type OnConflictValue struct {
|
||||
ID int
|
||||
Params datatypes.JSONMap `gorm:"default:'{}'"`
|
||||
}
|
||||
|
||||
err := DB.Migrator().DropTable(&OnConflictValue{})
|
||||
AssertEqual(t, err, nil)
|
||||
err = DB.AutoMigrate(&OnConflictValue{})
|
||||
AssertEqual(t, err, nil)
|
||||
|
||||
v := OnConflictValue{
|
||||
Params: datatypes.JSONMap{"foo": "bar"},
|
||||
}
|
||||
err = DB.Create(&v).Error
|
||||
AssertEqual(t, err, nil)
|
||||
|
||||
err = DB.Clauses(clause.OnConflict{UpdateAll: true}).Create(&OnConflictValue{
|
||||
ID: v.ID,
|
||||
Params: datatypes.JSONMap{"foo": "new-bar"},
|
||||
}).Error
|
||||
AssertEqual(t, err, nil)
|
||||
|
||||
var v2 OnConflictValue
|
||||
err = DB.Where("id = ?", v.ID).First(&v2).Error
|
||||
AssertEqual(t, err, nil)
|
||||
AssertEqual(t, v2.Params, datatypes.JSONMap{"foo": "new-bar"})
|
||||
}
|
||||
|
||||
func TestCreateFromMapWithoutPK(t *testing.T) {
|
||||
if !isMysql() {
|
||||
t.Skipf("This test case skipped, because of only supporting for mysql")
|
||||
|
|
|
@ -8,7 +8,7 @@ require (
|
|||
github.com/lib/pq v1.10.9
|
||||
github.com/stretchr/testify v1.9.0
|
||||
gorm.io/driver/mysql v1.5.7
|
||||
gorm.io/driver/postgres v1.5.9
|
||||
gorm.io/driver/postgres v1.5.10
|
||||
gorm.io/driver/sqlite v1.5.6
|
||||
gorm.io/driver/sqlserver v1.5.4
|
||||
gorm.io/gorm v1.25.12
|
||||
|
@ -25,12 +25,12 @@ require (
|
|||
github.com/jackc/pgx/v5 v5.7.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.23 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.24 // indirect
|
||||
github.com/microsoft/go-mssqldb v1.7.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.12.0 // indirect
|
||||
golang.org/x/crypto v0.27.0 // indirect
|
||||
golang.org/x/text v0.18.0 // indirect
|
||||
golang.org/x/crypto v0.29.0 // indirect
|
||||
golang.org/x/text v0.20.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
|
|
|
@ -16,22 +16,23 @@ then
|
|||
fi
|
||||
|
||||
# SqlServer for Mac M1
|
||||
if [[ -z $GITHUB_ACTION ]]; then
|
||||
if [ -d tests ]
|
||||
then
|
||||
if [[ -z $GITHUB_ACTION && -d tests ]]; then
|
||||
cd tests
|
||||
if [[ $(uname -a) == *" arm64" ]]; then
|
||||
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d || true
|
||||
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d --wait
|
||||
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
|
||||
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" > /dev/null || true
|
||||
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" > /dev/null || true
|
||||
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" > /dev/null || true
|
||||
for query in \
|
||||
"IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" \
|
||||
"IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" \
|
||||
"IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];"
|
||||
do
|
||||
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "$query" > /dev/null || true
|
||||
done
|
||||
else
|
||||
MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d
|
||||
MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d --wait
|
||||
fi
|
||||
cd ..
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
for dialect in "${dialects[@]}" ; do
|
||||
|
|
Loading…
Reference in New Issue