mirror of https://github.com/go-gorm/gorm.git
Merge branch 'master' into plugin_system
This commit is contained in:
commit
45edecd13b
9
field.go
9
field.go
|
@ -14,6 +14,7 @@ type Field struct {
|
|||
model *Model
|
||||
dbName string
|
||||
isBlank bool
|
||||
ignoreField bool
|
||||
isPrimaryKey bool
|
||||
autoCreateTime bool
|
||||
autoUpdateTime bool
|
||||
|
@ -28,6 +29,14 @@ func (f *Field) parseBlank() {
|
|||
f.isBlank = isBlank(f.reflectValue)
|
||||
}
|
||||
|
||||
func (f *Field) parseIgnore() {
|
||||
typ, _, _ := parseSqlTag(f.structField.Tag.Get(f.model.do.db.parent.tagIdentifier))
|
||||
|
||||
if typ == "-" {
|
||||
f.ignoreField = true
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Field) isScanner() bool {
|
||||
_, is_scanner := reflect.New(f.reflectValue.Type()).Interface().(sql.Scanner)
|
||||
return is_scanner
|
||||
|
|
46
gorm_test.go
46
gorm_test.go
|
@ -17,24 +17,29 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type IgnoredEmbedStruct struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id int64 // Id: Primary key
|
||||
Birthday time.Time // Time
|
||||
Age int64
|
||||
Name string `sql:"size:255"`
|
||||
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
|
||||
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically
|
||||
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more
|
||||
Emails []Email // Embedded structs
|
||||
BillingAddress Address // Embedded struct
|
||||
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
||||
ShippingAddress Address // Embedded struct
|
||||
ShippingAddressId int64 // Embedded struct's foreign key
|
||||
When time.Time
|
||||
CreditCard CreditCard
|
||||
Latitude float64
|
||||
PasswordHash []byte
|
||||
IgnoreMe int64 `sql:"-"`
|
||||
Id int64 // Id: Primary key
|
||||
Birthday time.Time // Time
|
||||
Age int64
|
||||
Name string `sql:"size:255"`
|
||||
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
|
||||
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically
|
||||
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more
|
||||
Emails []Email // Embedded structs
|
||||
IgnoredEmbedStruct IgnoredEmbedStruct `sql:"-"`
|
||||
BillingAddress Address // Embedded struct
|
||||
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
||||
ShippingAddress Address // Embedded struct
|
||||
ShippingAddressId int64 // Embedded struct's foreign key
|
||||
When time.Time
|
||||
CreditCard CreditCard
|
||||
Latitude float64
|
||||
PasswordHash []byte
|
||||
IgnoreMe int64 `sql:"-"`
|
||||
}
|
||||
|
||||
type CreditCard struct {
|
||||
|
@ -1251,6 +1256,13 @@ func TestSubStruct(t *testing.T) {
|
|||
db.Save(&comment3)
|
||||
}
|
||||
|
||||
func TestIgnoreAssociation(t *testing.T) {
|
||||
user := User{Name: "ignore", IgnoredEmbedStruct: IgnoredEmbedStruct{Name: "IgnoreMe"}}
|
||||
if err := db.Save(&user).Error; err != nil {
|
||||
t.Errorf("Should have no error with ignored association, but got ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelated(t *testing.T) {
|
||||
user := User{
|
||||
Name: "jinzhu",
|
||||
|
|
5
model.go
5
model.go
|
@ -102,6 +102,7 @@ func (m *Model) fields(operation string) (fields []*Field) {
|
|||
field.Value = value.Interface()
|
||||
field.parseAssociation()
|
||||
field.parseBlank()
|
||||
field.parseIgnore()
|
||||
c <- &field
|
||||
}(field_struct, c)
|
||||
}
|
||||
|
@ -271,7 +272,7 @@ func (m *Model) setValueByColumn(name string, value interface{}, out interface{}
|
|||
|
||||
func (m *Model) beforeAssociations() (fields []*Field) {
|
||||
for _, field := range m.fields("null") {
|
||||
if field.beforeAssociation && !field.isBlank {
|
||||
if field.beforeAssociation && !field.isBlank && !field.ignoreField {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
}
|
||||
|
@ -280,7 +281,7 @@ func (m *Model) beforeAssociations() (fields []*Field) {
|
|||
|
||||
func (m *Model) afterAssociations() (fields []*Field) {
|
||||
for _, field := range m.fields("null") {
|
||||
if field.afterAssociation && !field.isBlank {
|
||||
if field.afterAssociation && !field.isBlank && !field.ignoreField {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue