mirror of https://github.com/go-gorm/gorm.git
Test Save with specfied table name
This commit is contained in:
parent
c44c6027fb
commit
153ce22c99
2
main.go
2
main.go
|
@ -466,7 +466,7 @@ func (s *DB) Save(value interface{}) *DB {
|
|||
if !scope.PrimaryKeyZero() {
|
||||
newDB := scope.callCallbacks(s.parent.callbacks.updates).db
|
||||
if newDB.Error == nil && newDB.RowsAffected == 0 {
|
||||
return s.FirstOrCreate(value)
|
||||
return s.New().Table(scope.TableName()).FirstOrCreate(value)
|
||||
}
|
||||
return newDB
|
||||
}
|
||||
|
|
15
main_test.go
15
main_test.go
|
@ -44,13 +44,13 @@ func OpenTestConnection() (db *gorm.DB, err error) {
|
|||
case "mysql":
|
||||
fmt.Println("testing mysql...")
|
||||
if dbDSN == "" {
|
||||
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True"
|
||||
dbDSN = "gorm:gorm@tcp(localhost:3306)/gorm?charset=utf8&parseTime=True"
|
||||
}
|
||||
db, err = gorm.Open("mysql", dbDSN)
|
||||
case "postgres":
|
||||
fmt.Println("testing postgres...")
|
||||
if dbDSN == "" {
|
||||
dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable"
|
||||
dbDSN = "user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable"
|
||||
}
|
||||
db, err = gorm.Open("postgres", dbDSN)
|
||||
case "mssql":
|
||||
|
@ -61,7 +61,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {
|
|||
// sp_changedbowner 'gorm';
|
||||
fmt.Println("testing mssql...")
|
||||
if dbDSN == "" {
|
||||
dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
|
||||
dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:1433?database=gorm"
|
||||
}
|
||||
db, err = gorm.Open("mssql", dbDSN)
|
||||
default:
|
||||
|
@ -178,6 +178,15 @@ func TestSetTable(t *testing.T) {
|
|||
t.Errorf("Query from specified table")
|
||||
}
|
||||
|
||||
var user User
|
||||
DB.Table("deleted_users").First(&user, "name = ?", "DeletedUser")
|
||||
|
||||
user.Age = 20
|
||||
DB.Table("deleted_users").Save(&user)
|
||||
if DB.Table("deleted_users").First(&user, "name = ? AND age = ?", "DeletedUser", 20).RecordNotFound() {
|
||||
t.Errorf("Failed to found updated user")
|
||||
}
|
||||
|
||||
DB.Save(getPreparedUser("normal_user", "reset_table"))
|
||||
DB.Table("deleted_users").Save(getPreparedUser("deleted_user", "reset_table"))
|
||||
var user1, user2, user3 User
|
||||
|
|
Loading…
Reference in New Issue