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 關注
- 317 瀏覽
添加回答
舉報