聚会有点晚了,但是……我的目标是创造类似的东西,花了一整天的时间,碰壁后,退后一步重新考虑……
经过深思熟虑,很多游戏,以及朋友的好建议,决定从稍微不同的角度接近。
如果您以不同的方式看待它,您可以将其“改写”为两个 has-one(或 belongs-to)和两个 one-to-many,而不是多对多:
- 运动有很多锻炼-运动
- 锻炼有很多锻炼-锻炼
- workout-exercise 有锻炼和一项运动
另外,在我的游戏中,我意识到多对多表的属性(列)在上述模型中的任何地方都没有引用,因此无论如何它都没有用。
如果您将锻炼-锻炼视为“一流”模型,它开始变得更有意义并变得更实用:
- 您可以创建练习 - 它是一种静态资源池
- 您可以创建锻炼方式
- 然后您可以使用锻炼-锻炼模型在锻炼中填充锻炼。
总而言之,我的建议是这样的:
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 := Workout
db.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)
}
*免责声明 - 没有实际运行此代码(其余的工作)。请查看Nested Preloading 以实现一些深度嵌套的急切加载。