forked from mirror/gorm
Ignore association with sql tag
This commit is contained in:
parent
0459aa89db
commit
2409c26710
9
field.go
9
field.go
|
@ -14,6 +14,7 @@ type Field struct {
|
||||||
model *Model
|
model *Model
|
||||||
dbName string
|
dbName string
|
||||||
isBlank bool
|
isBlank bool
|
||||||
|
ignoreField bool
|
||||||
isPrimaryKey bool
|
isPrimaryKey bool
|
||||||
autoCreateTime bool
|
autoCreateTime bool
|
||||||
autoUpdateTime bool
|
autoUpdateTime bool
|
||||||
|
@ -28,6 +29,14 @@ func (f *Field) parseBlank() {
|
||||||
f.isBlank = isBlank(f.reflectValue)
|
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 {
|
func (f *Field) isScanner() bool {
|
||||||
_, is_scanner := reflect.New(f.reflectValue.Type()).Interface().(sql.Scanner)
|
_, is_scanner := reflect.New(f.reflectValue.Type()).Interface().(sql.Scanner)
|
||||||
return is_scanner
|
return is_scanner
|
||||||
|
|
46
gorm_test.go
46
gorm_test.go
|
@ -17,24 +17,29 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type IgnoredEmbedStruct struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id int64 // Id: Primary key
|
Id int64 // Id: Primary key
|
||||||
Birthday time.Time // Time
|
Birthday time.Time // Time
|
||||||
Age int64
|
Age int64
|
||||||
Name string `sql:"size:255"`
|
Name string `sql:"size:255"`
|
||||||
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
|
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
|
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
|
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more
|
||||||
Emails []Email // Embedded structs
|
Emails []Email // Embedded structs
|
||||||
BillingAddress Address // Embedded struct
|
IgnoredEmbedStruct IgnoredEmbedStruct `sql:"-"`
|
||||||
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
BillingAddress Address // Embedded struct
|
||||||
ShippingAddress Address // Embedded struct
|
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
||||||
ShippingAddressId int64 // Embedded struct's foreign key
|
ShippingAddress Address // Embedded struct
|
||||||
When time.Time
|
ShippingAddressId int64 // Embedded struct's foreign key
|
||||||
CreditCard CreditCard
|
When time.Time
|
||||||
Latitude float64
|
CreditCard CreditCard
|
||||||
PasswordHash []byte
|
Latitude float64
|
||||||
IgnoreMe int64 `sql:"-"`
|
PasswordHash []byte
|
||||||
|
IgnoreMe int64 `sql:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreditCard struct {
|
type CreditCard struct {
|
||||||
|
@ -1249,6 +1254,13 @@ func TestSubStruct(t *testing.T) {
|
||||||
db.Save(&comment3)
|
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) {
|
func TestRelated(t *testing.T) {
|
||||||
user := User{
|
user := User{
|
||||||
Name: "jinzhu",
|
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.Value = value.Interface()
|
||||||
field.parseAssociation()
|
field.parseAssociation()
|
||||||
field.parseBlank()
|
field.parseBlank()
|
||||||
|
field.parseIgnore()
|
||||||
c <- &field
|
c <- &field
|
||||||
}(field_struct, c)
|
}(field_struct, c)
|
||||||
}
|
}
|
||||||
|
@ -271,7 +272,7 @@ func (m *Model) setValueByColumn(name string, value interface{}, out interface{}
|
||||||
|
|
||||||
func (m *Model) beforeAssociations() (fields []*Field) {
|
func (m *Model) beforeAssociations() (fields []*Field) {
|
||||||
for _, field := range m.fields("null") {
|
for _, field := range m.fields("null") {
|
||||||
if field.beforeAssociation && !field.isBlank {
|
if field.beforeAssociation && !field.isBlank && !field.ignoreField {
|
||||||
fields = append(fields, field)
|
fields = append(fields, field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -280,7 +281,7 @@ func (m *Model) beforeAssociations() (fields []*Field) {
|
||||||
|
|
||||||
func (m *Model) afterAssociations() (fields []*Field) {
|
func (m *Model) afterAssociations() (fields []*Field) {
|
||||||
for _, field := range m.fields("null") {
|
for _, field := range m.fields("null") {
|
||||||
if field.afterAssociation && !field.isBlank {
|
if field.afterAssociation && !field.isBlank && !field.ignoreField {
|
||||||
fields = append(fields, field)
|
fields = append(fields, field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue