forked from mirror/gorm
Error message when set primary key
This commit is contained in:
parent
2d40175651
commit
5b671a84b6
4
do.go
4
do.go
|
@ -159,7 +159,9 @@ func (s *Do) create() {
|
||||||
|
|
||||||
if !s.hasError() {
|
if !s.hasError() {
|
||||||
result := reflect.Indirect(reflect.ValueOf(s.value))
|
result := reflect.Indirect(reflect.ValueOf(s.value))
|
||||||
setFieldValue(result.FieldByName(s.model.primaryKey()), id)
|
if !setFieldValue(result.FieldByName(s.model.primaryKey()), id) {
|
||||||
|
fmt.Printf("Can't set primary key for %#v\n", result.Interface())
|
||||||
|
}
|
||||||
s.saveAfterAssociations()
|
s.saveAfterAssociations()
|
||||||
|
|
||||||
s.err(s.model.callMethod("AfterCreate"))
|
s.err(s.model.callMethod("AfterCreate"))
|
||||||
|
|
12
gorm_test.go
12
gorm_test.go
|
@ -1002,15 +1002,17 @@ type Category struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
Id int64
|
Id int64
|
||||||
Title string
|
CategoryId int64
|
||||||
Body string
|
Title string
|
||||||
Comments []Comment
|
Body string
|
||||||
Category Category
|
Comments []Comment
|
||||||
|
Category Category
|
||||||
}
|
}
|
||||||
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
Id int64
|
Id int64
|
||||||
|
PostId int64
|
||||||
Content string
|
Content string
|
||||||
Post Post
|
Post Post
|
||||||
}
|
}
|
||||||
|
|
5
model.go
5
model.go
|
@ -309,7 +309,7 @@ func (m *Model) afterAssociations() (fields []Field) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFieldValue(field reflect.Value, value interface{}) {
|
func setFieldValue(field reflect.Value, value interface{}) bool {
|
||||||
if field.IsValid() && field.CanAddr() {
|
if field.IsValid() && field.CanAddr() {
|
||||||
switch field.Kind() {
|
switch field.Kind() {
|
||||||
case reflect.Int, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int32, reflect.Int64:
|
||||||
|
@ -320,5 +320,8 @@ func setFieldValue(field reflect.Value, value interface{}) {
|
||||||
default:
|
default:
|
||||||
field.Set(reflect.ValueOf(value))
|
field.Set(reflect.ValueOf(value))
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue