Skip to content

Instantly share code, notes, and snippets.

@islishude
Last active August 18, 2019 13:08
Show Gist options
  • Save islishude/c5419e4ade455e2e6a8c37488a724a6a to your computer and use it in GitHub Desktop.
Save islishude/c5419e4ade455e2e6a8c37488a724a6a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
// Student table
type Student struct {
gorm.Model
Name string `gorm:"not null;index"`
Teachers []*Teacher `gorm:"many2many:student_teacher"`
}
// Teacher table
type Teacher struct {
gorm.Model
Name string `gorm:"not null;index"`
Students []*Student `gorm:"many2many:student_teacher"`
}
func main() {
db, err := gorm.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test?parseTime=true")
if err != nil {
log.Fatalln("failed to connect database", err)
}
defer db.Close()
// db.Debug().AutoMigrate(new(Student), new(Teacher))
// craete
{
/*
mysql> select * from students;
+----+---------------------+---------------------+------------+------+
| id | created_at | updated_at | deleted_at | name |
+----+---------------------+---------------------+------------+------+
| 1 | 2019-08-18 12:41:31 | 2019-08-18 12:41:31 | NULL | st_a |
| 2 | 2019-08-18 12:41:31 | 2019-08-18 12:41:31 | NULL | st_b |
| 3 | 2019-08-18 12:41:31 | 2019-08-18 12:41:31 | NULL | st_c |
+----+---------------------+---------------------+------------+------+
3 rows in set (0.00 sec)
mysql> select * from teachers;
+----+---------------------+---------------------+------------+------+
| id | created_at | updated_at | deleted_at | name |
+----+---------------------+---------------------+------------+------+
| 1 | 2019-08-18 12:41:31 | 2019-08-18 12:41:31 | NULL | t_a |
+----+---------------------+---------------------+------------+------+
1 row in set (0.00 sec)
mysql> select * from student_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
+------------+------------+
3 rows in set (0.00 sec)
*/
t := Teacher{
Name: "t_a",
Students: []*Student{
&Student{Name: "st_a"},
&Student{Name: "st_b"},
&Student{Name: "st_c"},
},
}
db.Debug().Create(&t)
}
{
var teacher Teacher
db.Debug().First(&teacher, 1)
db.Debug().Model(&teacher).Related(&teacher.Students, "Students")
fmt.Printf("%+v", teacher)
fmt.Println()
}
{
var teacher Teacher
db.Debug().First(&teacher, 1)
/*
[2019-08-18 21:03:10] [6.94ms] INSERT INTO `student_teacher` (`student_id`,`teacher_id`) SELECT 1,1 FROM DUAL WHERE NOT EXISTS (SELECT * FROM `student_teacher` WHERE `student_id` = 1 AND `teacher_id` = 1)
[1 rows affected or returned ]
[2019-08-18 21:03:10] [5.61ms] INSERT INTO `student_teacher` (`teacher_id`,`student_id`) SELECT 1,2 FROM DUAL WHERE NOT EXISTS (SELECT * FROM `student_teacher` WHERE `teacher_id` = 1 AND `student_id` = 2)
[1 rows affected or returned ]
[2019-08-18 21:03:10] [4.42ms] INSERT INTO `student_teacher` (`teacher_id`,`student_id`) SELECT 1,3 FROM DUAL WHERE NOT EXISTS (SELECT * FROM `student_teacher` WHERE `teacher_id` = 1 AND `student_id` = 3)
[1 rows affected or returned ]
[2019-08-18 21:03:10] [1.81ms] DELETE FROM `student_teacher` WHERE (`student_id` NOT IN (1,2,3)) AND (`teacher_id` IN (1))
[3 rows affected or returned ]
*/
db.Debug().Model(&teacher).Association("Students").Replace([]*Student{
&Student{Model: gorm.Model{ID: 1}},
&Student{Model: gorm.Model{ID: 2}},
&Student{Model: gorm.Model{ID: 3}},
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment