From 189a22c4169ee518cd4aacba4b0cf885f476e794 Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Tue, 6 Aug 2024 23:01:39 +0800 Subject: [PATCH 01/10] feat: search by teacher name (PERF SUCKS) --- danke/api/course_group.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/danke/api/course_group.go b/danke/api/course_group.go index 25c5c0b..d616a84 100644 --- a/danke/api/course_group.go +++ b/danke/api/course_group.go @@ -2,6 +2,7 @@ package api import ( "errors" + "github.com/gofiber/fiber/v2" . "github.com/opentreehole/backend/common" . "github.com/opentreehole/backend/danke/model" @@ -148,7 +149,9 @@ func SearchCourseGroupV3(c *fiber.Ctx) (err error) { if CourseCodeRegexp.MatchString(query) { querySet = querySet.Where("code LIKE ?", query+"%") } else { - querySet = querySet.Where("name LIKE ?", "%"+query+"%") + // 查询教师名,先在课程表获取含有该教师名的课程所对应课程组编号列表,然后按所得编码查询主表 + subQuery := querySet.Select("code").Where("teachers LIKE ?", "%"+query+"%").Table("course") + querySet = querySet.Where("name LIKE ? OR code IN (?)", "%"+query+"%", subQuery) } if page > 0 { if pageSize == 0 { From daf5b8c1eb6741db7a041d8f87772ec9c93efe00 Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Thu, 8 Aug 2024 22:21:25 +0800 Subject: [PATCH 02/10] fix: improve performance --- danke/api/course_group.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/danke/api/course_group.go b/danke/api/course_group.go index d616a84..bd3de5b 100644 --- a/danke/api/course_group.go +++ b/danke/api/course_group.go @@ -150,8 +150,8 @@ func SearchCourseGroupV3(c *fiber.Ctx) (err error) { querySet = querySet.Where("code LIKE ?", query+"%") } else { // 查询教师名,先在课程表获取含有该教师名的课程所对应课程组编号列表,然后按所得编码查询主表 - subQuery := querySet.Select("code").Where("teachers LIKE ?", "%"+query+"%").Table("course") - querySet = querySet.Where("name LIKE ? OR code IN (?)", "%"+query+"%", subQuery) + subQuery := querySet.Select("course_group_id").Where("teacher LIKE ?", "%"+query+"%").Table("teacher") + querySet = querySet.Where("name LIKE ? OR id IN (?)", "%"+query+"%", subQuery) } if page > 0 { if pageSize == 0 { From 31b2d94d370e403fd8b4f245692e04707a9a26fd Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:09:29 +0800 Subject: [PATCH 03/10] fix: improve performance by using join --- danke/api/course_group.go | 9 ++- danke_utils/teacher_table.go | 141 +++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 danke_utils/teacher_table.go diff --git a/danke/api/course_group.go b/danke/api/course_group.go index bd3de5b..cf5e4ff 100644 --- a/danke/api/course_group.go +++ b/danke/api/course_group.go @@ -149,9 +149,12 @@ func SearchCourseGroupV3(c *fiber.Ctx) (err error) { if CourseCodeRegexp.MatchString(query) { querySet = querySet.Where("code LIKE ?", query+"%") } else { - // 查询教师名,先在课程表获取含有该教师名的课程所对应课程组编号列表,然后按所得编码查询主表 - subQuery := querySet.Select("course_group_id").Where("teacher LIKE ?", "%"+query+"%").Table("teacher") - querySet = querySet.Where("name LIKE ? OR id IN (?)", "%"+query+"%", subQuery) + queryWord :="%"+query+"%" + querySet = querySet. + Joins("JOIN teacher_courses tc ON course_group.id = tc.course_group_id"). + Joins("JOIN teacher t ON tc.teacher_id = t.id"). + Where("t.name like ? OR course_group.name LIKE ?", queryWord, queryWord). + Group("id") } if page > 0 { if pageSize == 0 { diff --git a/danke_utils/teacher_table.go b/danke_utils/teacher_table.go new file mode 100644 index 0000000..2bb7b65 --- /dev/null +++ b/danke_utils/teacher_table.go @@ -0,0 +1,141 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" + "time" + + "github.com/opentreehole/backend/common" + "github.com/spf13/viper" + "gorm.io/driver/mysql" + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/clause" + "gorm.io/gorm/logger" + "gorm.io/gorm/schema" +) + +const ( + BatchSize = 1000 +) + +var GormConfig = &gorm.Config{ + NamingStrategy: schema.NamingStrategy{ + SingularTable: true, // 表名使用单数, `User` -> `user` + }, + DisableForeignKeyConstraintWhenMigrating: true, // 禁用自动创建外键约束,必须手动创建或者在业务逻辑层维护 + Logger: logger.New( + log.New(os.Stdout, "\r\n", log.LstdFlags), + logger.Config{ + SlowThreshold: time.Second, // 慢 SQL 阈值 + LogLevel: logger.Error, // 日志级别 + IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误 + Colorful: false, // 禁用彩色打印 + }, + ), +} + +type Course struct { + ID int `json:"id"` + Name string `json:"name" gorm:"not null"` // 课程名称 + Code string `json:"code" gorm:"not null"` // 课程编号 + CodeID string `json:"code_id" gorm:"not null"` // 选课序号。用于区分同一课程编号的不同平行班 + Teachers string `json:"teachers" gorm:"not null"` + CourseGroupID int `json:"course_group_id" gorm:"not null;index"` // 课程组类型 +} + +type Teacher struct { + ID int + Name string `gorm:"not null"` // 课程组类型 +} + +type TeacherCourseLink struct { + TeacherID int `gorm:"primaryKey;autoIncrement:false"` + CourseGroupID int `gorm:"primaryKey;autoIncrement:false"` // 课程组类型 +} + +var DB *gorm.DB + +func Init() { + viper.AutomaticEnv() + dbType := viper.GetString(common.EnvDBType) + dbUrl := viper.GetString(common.EnvDBUrl) + + var err error + + switch dbType { + case "mysql": + DB, err = gorm.Open(mysql.Open(dbUrl), GormConfig) + case "postgres": + DB, err = gorm.Open(postgres.Open(dbUrl), GormConfig) + default: + panic("db type not supported") + } + + if err != nil { + panic(err) + } +} + +func main() { + Init() + + // reader := bufio.NewReader(os.Stdin) + + dataMap := map[string][]int{} + + var offset int = 0 + var queryResult []Course + for { + query := DB.Table("course").Limit(BatchSize).Offset(offset) + query.Find(&queryResult) + offset += len(queryResult) + + if len(queryResult) == 0 { + // Finished iterating + break + } + + for _, course := range queryResult { + teacherList := strings.Split(course.Teachers, ",") + for _, name := range teacherList { + courseList, found := dataMap[name] + if found { + dataMap[name] = append(courseList, course.CourseGroupID) + } else { + dataMap[name] = []int{course.CourseGroupID} + } + } + } + + // Avoid insertion failure due to duplication + + fmt.Printf("Handled %d records\n", offset) + // _, _ = reader.ReadString('\n') + } + + var teachers []*Teacher + for k := range dataMap { + teachers = append(teachers, &Teacher{Name: k}) + } + + DB.Clauses(clause.OnConflict{DoNothing: true}).Table("teacher").Create(teachers) + + var links []*TeacherCourseLink + for index, teacher := range teachers { + for _, cid := range dataMap[teacher.Name] { + links = append(links, &TeacherCourseLink{TeacherID: teacher.ID, CourseGroupID: cid}) + } + + // Submit every 100 teachers to avoid SQL being too long + if index%100 == 0 { + fmt.Printf("Inserted %d teachers\n", index) + + // Avoid insertion failure due to duplication + DB.Clauses(clause.OnConflict{DoNothing: true}).Table("teacher_courses").Create(links) + links = nil + } + } +} From b29551c8e66a339ba9987aa712b572442000b9f1 Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:15:06 +0800 Subject: [PATCH 04/10] chore: adjust directory structure --- danke_utils/main.go | 60 ++++++++++++++++++++++++++++++++++++ danke_utils/teacher_table.go | 50 +----------------------------- 2 files changed, 61 insertions(+), 49 deletions(-) create mode 100644 danke_utils/main.go diff --git a/danke_utils/main.go b/danke_utils/main.go new file mode 100644 index 0000000..b578c6e --- /dev/null +++ b/danke_utils/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "log" + "os" + "time" + + "github.com/opentreehole/backend/common" + "github.com/spf13/viper" + "gorm.io/driver/mysql" + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + "gorm.io/gorm/schema" +) + +var GormConfig = &gorm.Config{ + NamingStrategy: schema.NamingStrategy{ + SingularTable: true, // 表名使用单数, `User` -> `user` + }, + DisableForeignKeyConstraintWhenMigrating: true, // 禁用自动创建外键约束,必须手动创建或者在业务逻辑层维护 + Logger: logger.New( + log.New(os.Stdout, "\r\n", log.LstdFlags), + logger.Config{ + SlowThreshold: time.Second, // 慢 SQL 阈值 + LogLevel: logger.Error, // 日志级别 + IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误 + Colorful: false, // 禁用彩色打印 + }, + ), +} + +var DB *gorm.DB + +func Init() { + viper.AutomaticEnv() + dbType := viper.GetString(common.EnvDBType) + dbUrl := viper.GetString(common.EnvDBUrl) + + var err error + + switch dbType { + case "mysql": + DB, err = gorm.Open(mysql.Open(dbUrl), GormConfig) + case "postgres": + DB, err = gorm.Open(postgres.Open(dbUrl), GormConfig) + default: + panic("db type not supported") + } + + if err != nil { + panic(err) + } +} + +func main() { + Init() + // Call any script as needed + // GenerateTeacherTabele(DB) +} \ No newline at end of file diff --git a/danke_utils/teacher_table.go b/danke_utils/teacher_table.go index 2bb7b65..1615445 100644 --- a/danke_utils/teacher_table.go +++ b/danke_utils/teacher_table.go @@ -2,41 +2,16 @@ package main import ( "fmt" - "log" - "os" "strings" - "time" - "github.com/opentreehole/backend/common" - "github.com/spf13/viper" - "gorm.io/driver/mysql" - "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/clause" - "gorm.io/gorm/logger" - "gorm.io/gorm/schema" ) const ( BatchSize = 1000 ) -var GormConfig = &gorm.Config{ - NamingStrategy: schema.NamingStrategy{ - SingularTable: true, // 表名使用单数, `User` -> `user` - }, - DisableForeignKeyConstraintWhenMigrating: true, // 禁用自动创建外键约束,必须手动创建或者在业务逻辑层维护 - Logger: logger.New( - log.New(os.Stdout, "\r\n", log.LstdFlags), - logger.Config{ - SlowThreshold: time.Second, // 慢 SQL 阈值 - LogLevel: logger.Error, // 日志级别 - IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误 - Colorful: false, // 禁用彩色打印 - }, - ), -} - type Course struct { ID int `json:"id"` Name string `json:"name" gorm:"not null"` // 课程名称 @@ -56,30 +31,7 @@ type TeacherCourseLink struct { CourseGroupID int `gorm:"primaryKey;autoIncrement:false"` // 课程组类型 } -var DB *gorm.DB - -func Init() { - viper.AutomaticEnv() - dbType := viper.GetString(common.EnvDBType) - dbUrl := viper.GetString(common.EnvDBUrl) - - var err error - - switch dbType { - case "mysql": - DB, err = gorm.Open(mysql.Open(dbUrl), GormConfig) - case "postgres": - DB, err = gorm.Open(postgres.Open(dbUrl), GormConfig) - default: - panic("db type not supported") - } - - if err != nil { - panic(err) - } -} - -func main() { +func GenerateTeacherTabele(DB *gorm.DB) { Init() // reader := bufio.NewReader(os.Stdin) From ad7c7280346343c1b710b0aa3d708fe6bb606482 Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:29:39 +0800 Subject: [PATCH 05/10] fix: don't return deleted comments, resolves #134 --- danke/api/review.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/danke/api/review.go b/danke/api/review.go index 601aba7..ac3242f 100644 --- a/danke/api/review.go +++ b/danke/api/review.go @@ -2,6 +2,8 @@ package api import ( "errors" + "time" + "github.com/gofiber/fiber/v2" . "github.com/opentreehole/backend/common" "github.com/opentreehole/backend/common/sensitive" @@ -10,7 +12,6 @@ import ( "gorm.io/gorm" "gorm.io/gorm/clause" "gorm.io/plugin/dbresolver" - "time" ) // GetReviewV1 godoc @@ -70,7 +71,7 @@ func ListReviewsV1(c *fiber.Ctx) (err error) { // 查找评论 var reviews ReviewList - err = DB.Find(&reviews, "course_id = ?", courseID).Error + err = DB.Where("course_id = ? AND deleted_at IS NULL", courseID).Find(&reviews).Error if err != nil { return } From 81ac26e6e67703030adce26fdee36c0db5cc9e51 Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:40:57 +0800 Subject: [PATCH 06/10] change: rename the connection table --- danke/api/course_group.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/danke/api/course_group.go b/danke/api/course_group.go index cf5e4ff..35ef294 100644 --- a/danke/api/course_group.go +++ b/danke/api/course_group.go @@ -151,7 +151,7 @@ func SearchCourseGroupV3(c *fiber.Ctx) (err error) { } else { queryWord :="%"+query+"%" querySet = querySet. - Joins("JOIN teacher_courses tc ON course_group.id = tc.course_group_id"). + Joins("JOIN teacher_course_groups tcg ON course_group.id = tcg.course_group_id"). Joins("JOIN teacher t ON tc.teacher_id = t.id"). Where("t.name like ? OR course_group.name LIKE ?", queryWord, queryWord). Group("id") From 392ec10ce44aee7ae77eb772bf9ea2c30348038b Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Sun, 11 Aug 2024 11:11:58 +0800 Subject: [PATCH 07/10] Revert "fix: don't return deleted comments, resolves #134" This reverts commit ad7c7280346343c1b710b0aa3d708fe6bb606482. --- danke/api/review.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/danke/api/review.go b/danke/api/review.go index ac3242f..601aba7 100644 --- a/danke/api/review.go +++ b/danke/api/review.go @@ -2,8 +2,6 @@ package api import ( "errors" - "time" - "github.com/gofiber/fiber/v2" . "github.com/opentreehole/backend/common" "github.com/opentreehole/backend/common/sensitive" @@ -12,6 +10,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/clause" "gorm.io/plugin/dbresolver" + "time" ) // GetReviewV1 godoc @@ -71,7 +70,7 @@ func ListReviewsV1(c *fiber.Ctx) (err error) { // 查找评论 var reviews ReviewList - err = DB.Where("course_id = ? AND deleted_at IS NULL", courseID).Find(&reviews).Error + err = DB.Find(&reviews, "course_id = ?", courseID).Error if err != nil { return } From 3a86e2526cdeb3f1c3231280c267f2f59711b005 Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Wed, 18 Dec 2024 12:50:06 +0800 Subject: [PATCH 08/10] change: resolve code review issues --- danke_utils/main.go | 2 +- danke_utils/teacher_table.go | 37 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/danke_utils/main.go b/danke_utils/main.go index b578c6e..4df7dbe 100644 --- a/danke_utils/main.go +++ b/danke_utils/main.go @@ -56,5 +56,5 @@ func Init() { func main() { Init() // Call any script as needed - // GenerateTeacherTabele(DB) + // GenerateTeacherTable(DB) } \ No newline at end of file diff --git a/danke_utils/teacher_table.go b/danke_utils/teacher_table.go index 1615445..b662198 100644 --- a/danke_utils/teacher_table.go +++ b/danke_utils/teacher_table.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "slices" "strings" "gorm.io/gorm" @@ -31,48 +32,48 @@ type TeacherCourseLink struct { CourseGroupID int `gorm:"primaryKey;autoIncrement:false"` // 课程组类型 } -func GenerateTeacherTabele(DB *gorm.DB) { +func AppendUnique[T comparable](slice []T, elems ...T) []T { + for _, elem := range elems { + if !slices.Contains(slice, elem) { + slice = append(slice, elem) + } + } + + return slice +} + +func GenerateTeacherTable(DB *gorm.DB) { Init() // reader := bufio.NewReader(os.Stdin) dataMap := map[string][]int{} - var offset int = 0 var queryResult []Course - for { - query := DB.Table("course").Limit(BatchSize).Offset(offset) - query.Find(&queryResult) - offset += len(queryResult) - - if len(queryResult) == 0 { - // Finished iterating - break - } - + query := DB.Table("course") + query.FindInBatches(&queryResult, BatchSize, func(tx *gorm.DB, batch int) error { for _, course := range queryResult { teacherList := strings.Split(course.Teachers, ",") for _, name := range teacherList { courseList, found := dataMap[name] if found { - dataMap[name] = append(courseList, course.CourseGroupID) + dataMap[name] = AppendUnique(courseList, course.CourseGroupID) } else { dataMap[name] = []int{course.CourseGroupID} } } } - // Avoid insertion failure due to duplication - - fmt.Printf("Handled %d records\n", offset) - // _, _ = reader.ReadString('\n') - } + fmt.Printf("Handled batchg %d\n", batch) + return nil + }) var teachers []*Teacher for k := range dataMap { teachers = append(teachers, &Teacher{Name: k}) } + // Avoid insertion failure due to duplication DB.Clauses(clause.OnConflict{DoNothing: true}).Table("teacher").Create(teachers) var links []*TeacherCourseLink From b1663c2d00d3b28cf1d7685232e9c22b0baf70af Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Wed, 18 Dec 2024 22:56:15 +0800 Subject: [PATCH 09/10] add: `Teacher` model and related fields --- danke/model/course_group.go | 6 ++++-- danke/model/init.go | 1 + danke/model/teacher.go | 7 +++++++ danke_utils/teacher_table.go | 6 +++--- 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 danke/model/teacher.go diff --git a/danke/model/course_group.go b/danke/model/course_group.go index 2a40b24..ad84e5a 100644 --- a/danke/model/course_group.go +++ b/danke/model/course_group.go @@ -3,12 +3,13 @@ package model import ( "context" "encoding/base64" + "regexp" + "time" + "github.com/eko/gocache/lib/v4/store" "github.com/opentreehole/backend/common" "github.com/vmihailenco/msgpack/v5" "golang.org/x/crypto/sha3" - "regexp" - "time" ) // CourseGroup 课程组 @@ -24,6 +25,7 @@ type CourseGroup struct { CourseCount int `json:"course_count" gorm:"not null;default:0"` // 课程数量 ReviewCount int `json:"review_count" gorm:"not null;default:0"` // 评价数量 Courses CourseList `json:"courses"` + Teachers []*Teacher `gorm:"many2many:teacher_course_link;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` } var CourseCodeRegexp = regexp.MustCompile(`^([A-Z]{3,})([0-9]{2,})`) diff --git a/danke/model/init.go b/danke/model/init.go index 47092f0..580a0d0 100644 --- a/danke/model/init.go +++ b/danke/model/init.go @@ -40,6 +40,7 @@ func Init() { err = DB.AutoMigrate( &CourseGroup{}, &Course{}, + &Teacher{}, &Review{}, &ReviewHistory{}, &Achievement{}, diff --git a/danke/model/teacher.go b/danke/model/teacher.go new file mode 100644 index 0000000..8cbc848 --- /dev/null +++ b/danke/model/teacher.go @@ -0,0 +1,7 @@ +package model + +type Teacher struct { + ID int `json:"id"` + Name string `json:"name" gorm:"not null"` // 教师姓名 + CourseGroups []*CourseGroup `gorm:"many2many:teacher_course_link;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` +} \ No newline at end of file diff --git a/danke_utils/teacher_table.go b/danke_utils/teacher_table.go index b662198..bca6eeb 100644 --- a/danke_utils/teacher_table.go +++ b/danke_utils/teacher_table.go @@ -19,17 +19,17 @@ type Course struct { Code string `json:"code" gorm:"not null"` // 课程编号 CodeID string `json:"code_id" gorm:"not null"` // 选课序号。用于区分同一课程编号的不同平行班 Teachers string `json:"teachers" gorm:"not null"` - CourseGroupID int `json:"course_group_id" gorm:"not null;index"` // 课程组类型 + CourseGroupID int `json:"course_group_id" gorm:"not null;index"` // 课程组编号 } type Teacher struct { ID int - Name string `gorm:"not null"` // 课程组类型 + Name string `gorm:"not null"` // 课程组 ID } type TeacherCourseLink struct { TeacherID int `gorm:"primaryKey;autoIncrement:false"` - CourseGroupID int `gorm:"primaryKey;autoIncrement:false"` // 课程组类型 + CourseGroupID int `gorm:"primaryKey;autoIncrement:false"` // 课程组编号 } func AppendUnique[T comparable](slice []T, elems ...T) []T { From 4db24aeb20f6150eca354915f99631eada13ca30 Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Thu, 19 Dec 2024 07:53:52 +0800 Subject: [PATCH 10/10] fix: incosistent link table names --- danke/model/course_group.go | 2 +- danke/model/teacher.go | 2 +- danke_utils/teacher_table.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/danke/model/course_group.go b/danke/model/course_group.go index ad84e5a..96a0fca 100644 --- a/danke/model/course_group.go +++ b/danke/model/course_group.go @@ -25,7 +25,7 @@ type CourseGroup struct { CourseCount int `json:"course_count" gorm:"not null;default:0"` // 课程数量 ReviewCount int `json:"review_count" gorm:"not null;default:0"` // 评价数量 Courses CourseList `json:"courses"` - Teachers []*Teacher `gorm:"many2many:teacher_course_link;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + Teachers []*Teacher `gorm:"many2many:teacher_course_groups;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` } var CourseCodeRegexp = regexp.MustCompile(`^([A-Z]{3,})([0-9]{2,})`) diff --git a/danke/model/teacher.go b/danke/model/teacher.go index 8cbc848..f775338 100644 --- a/danke/model/teacher.go +++ b/danke/model/teacher.go @@ -3,5 +3,5 @@ package model type Teacher struct { ID int `json:"id"` Name string `json:"name" gorm:"not null"` // 教师姓名 - CourseGroups []*CourseGroup `gorm:"many2many:teacher_course_link;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + CourseGroups []*CourseGroup `gorm:"many2many:teacher_course_groups;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` } \ No newline at end of file diff --git a/danke_utils/teacher_table.go b/danke_utils/teacher_table.go index bca6eeb..a0c827b 100644 --- a/danke_utils/teacher_table.go +++ b/danke_utils/teacher_table.go @@ -87,7 +87,7 @@ func GenerateTeacherTable(DB *gorm.DB) { fmt.Printf("Inserted %d teachers\n", index) // Avoid insertion failure due to duplication - DB.Clauses(clause.OnConflict{DoNothing: true}).Table("teacher_courses").Create(links) + DB.Clauses(clause.OnConflict{DoNothing: true}).Table("teacher_course_groups").Create(links) links = nil } }