forked from mirror/gorm
db.Create() for force insert with predefined primary key
This commit is contained in:
parent
11f232a200
commit
d2e526bc2d
|
@ -237,6 +237,12 @@ db.Save(&user)
|
||||||
//// COMMIT;
|
//// COMMIT;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Create With Predefined Primary key
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.Create(&User{Id: 999, Name: "user 999"})
|
||||||
|
```
|
||||||
|
|
||||||
## Query
|
## Query
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
|
@ -26,8 +26,15 @@ func Create(scope *Scope) {
|
||||||
// set create sql
|
// set create sql
|
||||||
var sqls, columns []string
|
var sqls, columns []string
|
||||||
|
|
||||||
|
primaryKeyPredefined := false
|
||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored {
|
if field.DBName == scope.PrimaryKey() {
|
||||||
|
primaryKeyValue := fmt.Sprintf("%v", field.Value)
|
||||||
|
if primaryKeyValue != "" && primaryKeyValue != "0" {
|
||||||
|
primaryKeyPredefined = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(field.SqlTag) > 0 && !field.IsIgnored && (field.DBName != scope.PrimaryKey() || primaryKeyPredefined) {
|
||||||
columns = append(columns, scope.Quote(field.DBName))
|
columns = append(columns, scope.Quote(field.DBName))
|
||||||
sqls = append(sqls, scope.AddToVars(field.Value))
|
sqls = append(sqls, scope.AddToVars(field.Value))
|
||||||
}
|
}
|
||||||
|
@ -65,7 +72,7 @@ func Create(scope *Scope) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !scope.HasError() {
|
if !scope.HasError() && !primaryKeyPredefined {
|
||||||
scope.SetColumn(scope.PrimaryKey(), id)
|
scope.SetColumn(scope.PrimaryKey(), id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
5
main.go
5
main.go
|
@ -214,6 +214,11 @@ func (s *DB) Save(value interface{}) *DB {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DB) Create(value interface{}) *DB {
|
||||||
|
scope := s.clone().NewScope(value)
|
||||||
|
return scope.callCallbacks(s.parent.callback.creates).db
|
||||||
|
}
|
||||||
|
|
||||||
func (s *DB) Delete(value interface{}) *DB {
|
func (s *DB) Delete(value interface{}) *DB {
|
||||||
return s.clone().NewScope(value).callCallbacks(s.parent.callback.deletes).db
|
return s.clone().NewScope(value).callCallbacks(s.parent.callback.deletes).db
|
||||||
}
|
}
|
||||||
|
|
14
main_test.go
14
main_test.go
|
@ -2083,3 +2083,17 @@ func TestHstore(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreate(t *testing.T) {
|
||||||
|
if err := db.Create(&UserCompany{Id: 10, UserId: 1, CompanyId: 1}).Error; err != nil {
|
||||||
|
t.Error("Should be able to create record with predefined Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.First(&UserCompany{}, 10).RecordNotFound() {
|
||||||
|
t.Error("Record created with predefined primary key not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Create(&UserCompany{Id: 10, UserId: 10, CompanyId: 10}).Error; err == nil {
|
||||||
|
t.Error("Should not be able to create record with predefined duplicate Id")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue