package migrations import ( "gorm.io/gorm" "github.com/runningwater/gohub/app/models" "github.com/runningwater/gohub/pkg/migrate" ) func init() { type User struct { models.BaseModel } type Category struct { models.BaseModel } type Topic struct { models.BaseModel Title string `gorm:"type:varchar(255);not null;index;comment:标题"` Body string `gorm:"type:longtext;not null;comment:内容"` UserID uint `gorm:"type:bigint;not null; index;comment:用户ID"` CategoryID uint `gorm:"type:bigint;not null; index;comment:分类ID"` // 创建 user_id 和 category_id 外键关联 User User Category Category models.CommonTimestampsField } up := func(migrator gorm.Migrator, DB *gorm.DB) { _ = DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci").AutoMigrate(&Topic{}) } down := func(migrator gorm.Migrator, DB *gorm.DB) { _ = migrator.DropTable(&Topic{}) } migrate.Add(up, down, "2025_07_16_155731_add_topics_table") }