go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.03k stars 3.86k forks source link

多对多查询,中间表外键的问题 #7051

Open aboychen opened 3 weeks ago

aboychen commented 3 weeks ago

表1: type Speaker struct { Id int64 gorm:"primaryKey;autoIncrement;column:id" UserName string gorm:"size:50;column:user_name;comment:姓名" Avatar string gorm:"column:avatar;comment:头像" Gender string gorm:"column:gender;comment:性别" StyleName string gorm:"column:style_name;comment:调用名称" Domain []Dict gorm:"many2many:speaker_dict" Emotion []Dict gorm:"many2many:speaker_dict" Language []Dict gorm:"many2many:speaker_dict" } 表2: type Dict struct { Id int64 gorm:"primaryKey;autoIncrement;column:id" Channel string gorm:"size:64;column:channel;comment:频道" Name string gorm:"size:64;column:name;comment:英文名称" Title string gorm:"size:64;column:title;comment:中文名称" }

中间表: type SpeakerDict struct { SpeakerId int64 gorm:"column:speaker_id;comment:发音人编号" DictId int64 gorm:"column:dict_id;comment:字典编号" } 因为中间表的字典表是公用的 请问这样的多对多应该怎么写呢? 设置外键会提示中间domain_id不存在,但是我根本没这个字段,好像指定外键也不行

aboychen commented 3 weeks ago

目前我没办法做了六个数据库视图解决的,不清楚有没有简单一点的方法

github-actions[bot] commented 3 weeks ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking