亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Gorm 關聯多對多

Gorm 關聯多對多

Go
慕神8447489 2022-06-21 10:16:37
我知道 gorm 多對多關聯會創建一個連接表,但是如果我想要/需要一個帶有附加字段的自定義連接表怎么辦我下面的例子所以我有兩個 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 世界來說真的很新。
查看完整描述

2 回答

?
RISEBY

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)

}


查看完整回答
反對 回復 2022-06-21
?
阿晨1998

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) 


查看完整回答
反對 回復 2022-06-21
  • 2 回答
  • 0 關注
  • 317 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號