2 回答
TA贡献1856条经验 获得超5个赞
派对有点晚了,但是...我的目标是创造类似的东西,在花了一整天的时间之后,碰壁了,退后一步重新考虑它...
经过一番思考,很多游戏,以及朋友的好建议,决定从稍微不同的角度接近。
如果您以不同的方式看待它,而不是多对多,您可以将其“改写”为两个拥有一(或属于)和两个一对多:
运动有很多锻炼-运动
锻炼有很多锻炼锻炼
锻炼-锻炼有锻炼和一项锻炼
此外,在我的游戏中,我意识到多对多表的属性(列)在上面的模型中的任何地方都没有引用,所以无论如何它都没有用。
如果您将锻炼锻炼视为“一流”模型,它开始变得更有意义并变得更有用:
您可以创建练习 - 它是一种静态资源池
您可以创建锻炼
然后,您可以使用锻炼-锻炼模型在锻炼中填充锻炼。
总而言之,我的建议看起来像:
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)
}
TA贡献2037条经验 获得超6个赞
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 []Exercise
db.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 []Workout
db.Preload("Exercises").Find(&workouts)
- 2 回答
- 0 关注
- 286 浏览
添加回答
举报