Gorm 关联多对多

我知道 gorm 多对多关联会创建一个连接表,但是如果我想要/需要一个带有附加字段的自定义连接表怎么办

我下面的例子

http://img3.mukewang.com/62b12a1b0001cb1a11850318.jpg

所以我有两个 gorm 模型


type Exercise struct {

    gorm.Model

    Name        string `gorm:"not null" json:"name"`

    Description string `gorm:"not null json:"description"`

}

type Workout struct {

    gorm.Model

    Name string `gorm:"not null" json:"name"`

    CreatedAt time.Time `gorm:"not null" json:"created_at"`

}


如果我使用 gorm 多对多关联,它将创建一个锻炼锻炼连接表,但我将如何确保填写诸如 reps 和 set 之类的其他字段。这是需要在控制器中完成的事情,而不是真正的 gorm 处理。我用 Nodejs 和 sequelize 做过类似的事情,但对 go 和 gorm 世界来说真的很新。


慕神8447489
浏览 335回答 2
2回答

RISEBY

派对有点晚了,但是...我的目标是创造类似的东西,在花了一整天的时间之后,碰壁了,退后一步重新考虑它...经过一番思考,很多游戏,以及朋友的好建议,决定从稍微不同的角度接近。如果您以不同的方式看待它,而不是多对多,您可以将其“改写”为两个拥有一(或属于)和两个一对多:运动有很多锻炼-运动锻炼有很多锻炼锻炼锻炼-锻炼有锻炼和一项锻炼此外,在我的游戏中,我意识到多对多表的属性(列)在上面的模型中的任何地方都没有引用,所以无论如何它都没有用。如果您将锻炼锻炼视为“一流”模型,它开始变得更有意义并变得更有用:您可以创建练习 - 它是一种静态资源池您可以创建锻炼然后,您可以使用锻炼-锻炼模型在锻炼中填充锻炼。总而言之,我的建议看起来像:type Exercise struct {    gorm.Model    Name        string `gorm:"not null" json:"name"`    Description string `gorm:"not null" json:"description"`    Workouts    []WorkoutExercise}type Workout struct {    gorm.Model    Name      string    `gorm:"not null" json:"name"`    CreatedAt time.Time `gorm:"not null" json:"created_at"`    Exercises []WorkoutExercise}type WorkoutExercise struct {    gorm.Model    WorkoutID  uint    Workout    Workout `gorm:"foreignKey:WorkoutID;references:ID"`    ExerciseID uint    Exercise   Exercise `gorm:"foreignKey:ExerciseID;references:ID"`    Sets       uint    Reps       uint    Weights    uint}它产生以下架构(我相信这是需要的):gym=# \dt               List of relations Schema |       Name        | Type  |   Owner   --------+-------------------+-------+----------- public | installations     | table | gym public | workout_exercises | table | gym public | workouts          | table | gym(3 rows)gym=# \d workouts                                       Table "public.workouts"   Column   |           Type           | Collation | Nullable |               Default                ------------+--------------------------+-----------+----------+-------------------------------------- id         | bigint                   |           | not null | nextval('workouts_id_seq'::regclass) created_at | timestamp with time zone |           | not null |  updated_at | timestamp with time zone |           |          |  deleted_at | timestamp with time zone |           |          |  name       | text                     |           | not null | Indexes:    "workouts_pkey" PRIMARY KEY, btree (id)    "idx_workouts_deleted_at" btree (deleted_at)Referenced by:    TABLE "workout_exercises" CONSTRAINT "fk_workouts_exercises" FOREIGN KEY (workout_id) REFERENCES workouts(id)gym=# \d exercises                                       Table "public.exercises"   Column    |           Type           | Collation | Nullable |                Default                -------------+--------------------------+-----------+----------+--------------------------------------- id          | bigint                   |           | not null | nextval('exercises_id_seq'::regclass) created_at  | timestamp with time zone |           |          |  updated_at  | timestamp with time zone |           |          |  deleted_at  | timestamp with time zone |           |          |  name        | text                     |           | not null |  description | text                     |           | not null | Indexes:    "exercises_pkey" PRIMARY KEY, btree (id)    "idx_exercises_deleted_at" btree (deleted_at)Referenced by:    TABLE "workout_exercises" CONSTRAINT "fk_exercises_workouts" FOREIGN KEY (exercise_id) REFERENCES exercises(id)gym=# \d workout_exercises                                       Table "public.workout_exercises"   Column    |           Type           | Collation | Nullable |                    Default                    -------------+--------------------------+-----------+----------+----------------------------------------------- id          | bigint                   |           | not null | nextval('workout_exercises_id_seq'::regclass) created_at  | timestamp with time zone |           |          |  updated_at  | timestamp with time zone |           |          |  deleted_at  | timestamp with time zone |           |          |  workout_id  | bigint                   |           |          |  exercise_id | bigint                   |           |          |  sets        | bigint                   |           |          |  reps        | bigint                   |           |          |  weights     | bigint                   |           |          | Indexes:    "workout_exercises_pkey" PRIMARY KEY, btree (id)    "idx_workout_exercises_deleted_at" btree (deleted_at)Foreign-key constraints:    "fk_exercises_workouts" FOREIGN KEY (exercise_id) REFERENCES exercises(id)    "fk_workouts_exercises" FOREIGN KEY (workout_id) REFERENCES workouts(id)如果您考虑一下,将练习拉到锻炼中,而没有其他属性是没有意义的。如果您只想获得锻炼的练习,您可以从中间模型中提取它*:workout := Workoutdb.Preload("WorkoutExercise.Exercise").First(&workout, 10)for _, we := range workout.Exercises {    // access reps, etc directly from we    // and access the exercise from the property we.Exercise    log.Printf("do %d sets of %d repetitions of %s", we.Sets, we.Reps,                we.Exercise.Name)}

阿晨1998

type Exercise struct {    gorm.Model    Name        string `gorm:"not null" json:"name"`    Description string `gorm:"not null json:"description"`    Workouts []Workout `gorm:"many2many:exercise_workout;"`}type Workout struct {    gorm.Model    Name string `gorm:"not null" json:"name"`    CreatedAt time.Time `gorm:"not null" json:"created_at"`}many2many 的标记值是您定义的关联表名的名称。并使用以下代码加载带有锻炼数据的练习。var exercises []Exercisedb.Preload("Workouts").Find(&exercises) 如果你想用运动数据加载锻炼,只需在 Workout 结构中添加 many2many 关联,例如:type Exercise struct {    gorm.Model    Name        string `gorm:"not null" json:"name"`    Description string `gorm:"not null json:"description"`    Workouts []Workout `gorm:"many2many:exercise_workout;"`}type Workout struct {    gorm.Model    Name string `gorm:"not null" json:"name"`    CreatedAt time.Time `gorm:"not null" json:"created_at"`    Exercises []Exercise `gorm:"many2many:exercise_workout;"`}var workouts []Workoutdb.Preload("Exercises").Find(&workouts) 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go