Backreference, will ignore my custom join table
many2many When many2many customizes the join table, if it is backreferenced, the customized join table will be ignored. I don't know the author. Did you do it on purpose? Or accidentally?
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"time"
)
type User struct {
Id uint `gorm:"primaryKey"`
Name string
NickName string
Avatar string
Addresses []Address `gorm:"many2many:user_address;"`
}
type Address struct {
ID uint
Name string
// 当开启这个反向引用之后会忽略自定义的连接表 PersonAddress 从而使用 user_address 创建一个自定义的中间表 (你是故意忽略的? 还是不小心忽略的?)
// When this backreference is enabled, the custom join table PersonAddress is ignored to create a custom intermediate table using user_address
//Users []User `gorm:"many2many:user_address;"`
}
type PersonAddress struct {
UserID int `gorm:"primaryKey"`
AddressID int `gorm:"primaryKey"`
CreatedAt time.Time
DeletedAt gorm.DeletedAt
}
func (PersonAddress) TableName() string {
return "user_address"
}
func main() {
db, err := Connect()
if err != nil {
panic(err)
}
err = db.SetupJoinTable(&User{}, "Addresses", &PersonAddress{})
err = db.AutoMigrate(&User{})
if err != nil {
panic(err)
}
}
func Connect() (db *gorm.DB, err error) {
dsn := "root:123456@tcp(127.0.0.1:8089)/grom?charset=utf8mb4&parseTime=True&loc=Local"
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return db, err
}
return db, nil
}
go 1.19
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
gorm.io/driver/mysql v1.4.7 // indirect
gorm.io/gorm v1.24.6 // indirect
Expected answer
I think it should not be possible to ignore the custom join table.