forked from mirror/gorm
Test Related with search conditions
This commit is contained in:
parent
d7d9e24e1e
commit
1949baf5c8
|
@ -1351,10 +1351,17 @@ func TestRelated(t *testing.T) {
|
||||||
if len(emails) != 2 {
|
if len(emails) != 2 {
|
||||||
t.Errorf("Should have two emails")
|
t.Errorf("Should have two emails")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var emails2 []Email
|
||||||
|
db.Model(&user).Where("email = ?", "jinzhu@example.com").Related(&emails2)
|
||||||
|
if len(emails2) != 1 {
|
||||||
|
t.Errorf("Should have two emails")
|
||||||
|
}
|
||||||
|
|
||||||
var user1 User
|
var user1 User
|
||||||
db.Model(&user).Related(&user1.Emails)
|
db.Model(&user).Related(&user1.Emails)
|
||||||
if len(user1.Emails) != 2 {
|
if len(user1.Emails) != 2 {
|
||||||
t.Errorf("Should have two emails")
|
t.Errorf("Should have only one email match related condition")
|
||||||
}
|
}
|
||||||
|
|
||||||
var address1 Address
|
var address1 Address
|
||||||
|
|
|
@ -393,8 +393,7 @@ func (scope *Scope) typeName() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
||||||
toScope := scope.New(value)
|
toScope := scope.db.NewScope(value)
|
||||||
toScope.db = scope.db
|
|
||||||
|
|
||||||
for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
|
for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
|
||||||
if foreignValue, ok := scope.FieldByName(foreignKey); ok {
|
if foreignValue, ok := scope.FieldByName(foreignKey); ok {
|
||||||
|
|
Loading…
Reference in New Issue