forked from mirror/gorm
Write stub code for Many2Many
This commit is contained in:
parent
62fd13e04e
commit
2bffb43138
8
main.go
8
main.go
|
@ -338,7 +338,11 @@ func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB {
|
|||
return s
|
||||
}
|
||||
|
||||
func (s *DB) RemoveIndex(column string) *DB {
|
||||
s.clone().NewScope(s.Value).removeIndex(column)
|
||||
func (s *DB) RemoveIndex(indexName string) *DB {
|
||||
s.clone().NewScope(s.Value).removeIndex(indexName)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *DB) Many2Many(column string) *DB {
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package gorm
|
||||
|
||||
type many2many struct {
|
||||
}
|
||||
|
||||
func (*many2many) Find(value interface{}) {
|
||||
}
|
||||
|
||||
func (*many2many) Append(values interface{}) {
|
||||
}
|
||||
|
||||
func (*many2many) Delete(value interface{}) {
|
||||
}
|
||||
|
||||
func (*many2many) Clear(value interface{}) {
|
||||
}
|
||||
|
||||
func (*many2many) Replace(values interface{}) {
|
||||
}
|
||||
|
||||
func (*many2many) Count(values interface{}) int {
|
||||
return 0
|
||||
}
|
|
@ -51,6 +51,10 @@ func runMigration() {
|
|||
if err := db.AutoMigrate(Role{}).Error; err != nil {
|
||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(Language{}).Error; err != nil {
|
||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexes(t *testing.T) {
|
||||
|
|
|
@ -127,10 +127,25 @@ func TestRelated(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestQueryManyToManyWithRelated(t *testing.T) {
|
||||
// db.Model(&User{}).Related(&[]Language{}, "Languages")
|
||||
// SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111
|
||||
// db.Model(&User{}).Many2Many("Languages").Find(&[]Language{})
|
||||
var languages = []Language{{Name: "ZH"}, {Name: "EN"}, {Name: "DE"}}
|
||||
user := User{Name: "Many2Many", Languages: languages}
|
||||
db.Debug().Save(&user)
|
||||
|
||||
var newLanguages []Language
|
||||
db.Model(&user).Related(&newLanguages, "Languages")
|
||||
if len(newLanguages) != 3 {
|
||||
t.Errorf("Query many to many relations")
|
||||
}
|
||||
|
||||
newLanguages = []Language{}
|
||||
db.Model(&user).Many2Many("Languages").Find(&newLanguages)
|
||||
if len(newLanguages) != 3 {
|
||||
t.Errorf("Query many to many relations")
|
||||
}
|
||||
|
||||
// db.Model(&User{}).Many2Many("Languages").Add(&Language{})
|
||||
// db.Model(&User{}).Many2Many("Languages").Remove(&Language{})
|
||||
// db.Model(&User{}).Many2Many("Languages").Replace(&[]Language{})
|
||||
// db.Model(&User{}).Related(&[]Language{}, "Languages")
|
||||
// SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111
|
||||
}
|
||||
|
|
|
@ -300,7 +300,7 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore
|
|||
}
|
||||
|
||||
func (scope *Scope) sqlTagForField(field *Field) (tag string) {
|
||||
tag, addationalTag, size := parseSqlTag(field.Tag.Get(scope.db.parent.tagIdentifier))
|
||||
tag, additionalTag, size := parseSqlTag(field.Tag.Get(scope.db.parent.tagIdentifier))
|
||||
|
||||
if tag == "-" {
|
||||
field.IsIgnored = true
|
||||
|
@ -330,8 +330,8 @@ func (scope *Scope) sqlTagForField(field *Field) (tag string) {
|
|||
}
|
||||
}
|
||||
|
||||
if len(addationalTag) > 0 {
|
||||
tag = tag + " " + addationalTag
|
||||
if len(additionalTag) > 0 {
|
||||
tag = tag + " " + additionalTag
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ type User struct {
|
|||
ShippingAddressId int64 // Embedded struct's foreign key
|
||||
CreditCard CreditCard
|
||||
Latitude float64
|
||||
Languages []Language `gorm:"many2many:user_languages;"`
|
||||
CompanyId int64
|
||||
Company
|
||||
Role
|
||||
|
@ -60,6 +61,11 @@ type Address struct {
|
|||
DeletedAt time.Time
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Id int
|
||||
Name string
|
||||
}
|
||||
|
||||
type Product struct {
|
||||
Id int64
|
||||
Code string
|
||||
|
|
Loading…
Reference in New Issue