mirror of https://github.com/go-gorm/gorm.git
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
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) RemoveIndex(column string) *DB {
|
func (s *DB) RemoveIndex(indexName string) *DB {
|
||||||
s.clone().NewScope(s.Value).removeIndex(column)
|
s.clone().NewScope(s.Value).removeIndex(indexName)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *DB) Many2Many(column string) *DB {
|
||||||
return s
|
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 {
|
if err := db.AutoMigrate(Role{}).Error; err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
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) {
|
func TestIndexes(t *testing.T) {
|
||||||
|
|
|
@ -127,10 +127,25 @@ func TestRelated(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryManyToManyWithRelated(t *testing.T) {
|
func TestQueryManyToManyWithRelated(t *testing.T) {
|
||||||
// db.Model(&User{}).Related(&[]Language{}, "Languages")
|
var languages = []Language{{Name: "ZH"}, {Name: "EN"}, {Name: "DE"}}
|
||||||
// SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111
|
user := User{Name: "Many2Many", Languages: languages}
|
||||||
// db.Model(&User{}).Many2Many("Languages").Find(&[]Language{})
|
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").Add(&Language{})
|
||||||
// db.Model(&User{}).Many2Many("Languages").Remove(&Language{})
|
// db.Model(&User{}).Many2Many("Languages").Remove(&Language{})
|
||||||
// db.Model(&User{}).Many2Many("Languages").Replace(&[]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) {
|
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 == "-" {
|
if tag == "-" {
|
||||||
field.IsIgnored = true
|
field.IsIgnored = true
|
||||||
|
@ -330,8 +330,8 @@ func (scope *Scope) sqlTagForField(field *Field) (tag string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(addationalTag) > 0 {
|
if len(additionalTag) > 0 {
|
||||||
tag = tag + " " + addationalTag
|
tag = tag + " " + additionalTag
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ type User struct {
|
||||||
ShippingAddressId int64 // Embedded struct's foreign key
|
ShippingAddressId int64 // Embedded struct's foreign key
|
||||||
CreditCard CreditCard
|
CreditCard CreditCard
|
||||||
Latitude float64
|
Latitude float64
|
||||||
|
Languages []Language `gorm:"many2many:user_languages;"`
|
||||||
CompanyId int64
|
CompanyId int64
|
||||||
Company
|
Company
|
||||||
Role
|
Role
|
||||||
|
@ -60,6 +61,11 @@ type Address struct {
|
||||||
DeletedAt time.Time
|
DeletedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Language struct {
|
||||||
|
Id int
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
type Product struct {
|
type Product struct {
|
||||||
Id int64
|
Id int64
|
||||||
Code string
|
Code string
|
||||||
|
|
Loading…
Reference in New Issue