Add JoinTableHandler test
This commit is contained in:
parent
6d64e6837b
commit
22b1a93e03
70
join_table_test.go
Normal file
70
join_table_test.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package gorm_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Id int
|
||||||
|
Name string
|
||||||
|
Addresses []*Address `gorm:"many2many:person_addresses;"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PersonAddress struct {
|
||||||
|
PersonID int
|
||||||
|
AddressID int
|
||||||
|
DeletedAt time.Time
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*PersonAddress) Add(db *gorm.DB, relationship *gorm.Relationship, foreignValue interface{}, associationValue interface{}) error {
|
||||||
|
return db.Where(map[string]interface{}{
|
||||||
|
relationship.ForeignDBName: foreignValue,
|
||||||
|
relationship.AssociationForeignDBName: associationValue,
|
||||||
|
}).Assign(map[string]interface{}{
|
||||||
|
relationship.ForeignFieldName: foreignValue,
|
||||||
|
relationship.AssociationForeignFieldName: associationValue,
|
||||||
|
"DeletedAt": gorm.Expr("NULL"),
|
||||||
|
}).FirstOrCreate(&PersonAddress{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*PersonAddress) Delete(db *gorm.DB, relationship *gorm.Relationship) error {
|
||||||
|
return db.Delete(&PersonAddress{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*PersonAddress) Scope(db *gorm.DB, relationship *gorm.Relationship) *gorm.DB {
|
||||||
|
return db.Where(fmt.Sprintf("%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02'", relationship.JoinTable, relationship.JoinTable))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJoinTable(t *testing.T) {
|
||||||
|
DB.Exec("drop table person_addresses;")
|
||||||
|
DB.AutoMigrate(&Person{})
|
||||||
|
DB.SetJoinTableHandler(&PersonAddress{}, "person_addresses")
|
||||||
|
|
||||||
|
address1 := &Address{Address1: "address 1"}
|
||||||
|
address2 := &Address{Address1: "address 2"}
|
||||||
|
person := &Person{Name: "person", Addresses: []*Address{address1, address2}}
|
||||||
|
DB.Save(person)
|
||||||
|
|
||||||
|
DB.Model(person).Association("Addresses").Delete(address1)
|
||||||
|
|
||||||
|
if DB.Find(&[]PersonAddress{}, "person_id = ?", person.Id).RowsAffected != 1 {
|
||||||
|
t.Errorf("Should found one address")
|
||||||
|
}
|
||||||
|
|
||||||
|
if DB.Model(person).Association("Addresses").Count() != 1 {
|
||||||
|
t.Errorf("Should found one address")
|
||||||
|
}
|
||||||
|
|
||||||
|
if DB.Unscoped().Find(&[]PersonAddress{}, "person_id = ?", person.Id).RowsAffected != 2 {
|
||||||
|
t.Errorf("Found two addresses with Unscoped")
|
||||||
|
}
|
||||||
|
|
||||||
|
if DB.Model(person).Association("Addresses").Clear(); DB.Model(person).Association("Addresses").Count() != 0 {
|
||||||
|
t.Errorf("Should deleted all addresses")
|
||||||
|
}
|
||||||
|
}
|
1
main.go
1
main.go
@ -481,6 +481,7 @@ func (s *DB) SetJoinTableHandler(joinTableHandler JoinTableHandler, tables ...st
|
|||||||
if len(tables) > 0 {
|
if len(tables) > 0 {
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
s.parent.joinTableHandlers[table] = joinTableHandler
|
s.parent.joinTableHandlers[table] = joinTableHandler
|
||||||
|
s.Table(table).AutoMigrate(joinTableHandler)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
s.parent.joinTableHandlers["*"] = joinTableHandler
|
s.parent.joinTableHandlers["*"] = joinTableHandler
|
||||||
|
@ -439,18 +439,17 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) createJoinTable(field *StructField) {
|
func (scope *Scope) createJoinTable(field *StructField) {
|
||||||
if field.Relationship != nil && field.Relationship.JoinTable != "" {
|
if relationship := field.Relationship; relationship != nil && relationship.JoinTable != "" {
|
||||||
if !scope.Dialect().HasTable(scope, field.Relationship.JoinTable) {
|
if !scope.Dialect().HasTable(scope, relationship.JoinTable) {
|
||||||
newScope := scope.db.NewScope("")
|
|
||||||
primaryKeySqlType := scope.Dialect().SqlTag(scope.PrimaryKeyField().Field, 255)
|
primaryKeySqlType := scope.Dialect().SqlTag(scope.PrimaryKeyField().Field, 255)
|
||||||
newScope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)",
|
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v)",
|
||||||
field.Relationship.JoinTable,
|
scope.Quote(relationship.JoinTable),
|
||||||
strings.Join([]string{
|
strings.Join([]string{
|
||||||
scope.Quote(field.Relationship.ForeignDBName) + " " + primaryKeySqlType,
|
scope.Quote(relationship.ForeignDBName) + " " + primaryKeySqlType,
|
||||||
scope.Quote(field.Relationship.AssociationForeignDBName) + " " + primaryKeySqlType}, ",")),
|
scope.Quote(relationship.AssociationForeignDBName) + " " + primaryKeySqlType}, ",")),
|
||||||
).Exec()
|
).Error)
|
||||||
scope.Err(newScope.db.Error)
|
|
||||||
}
|
}
|
||||||
|
scope.NewDB().Table(relationship.JoinTable).AutoMigrate(scope.db.GetJoinTableHandler(relationship.JoinTable))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user