2025_07_16_155731_add_topics_table.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package migrations
  2. import (
  3. "gorm.io/gorm"
  4. "github.com/runningwater/gohub/app/models"
  5. "github.com/runningwater/gohub/pkg/migrate"
  6. )
  7. func init() {
  8. type User struct {
  9. models.BaseModel
  10. }
  11. type Category struct {
  12. models.BaseModel
  13. }
  14. type Topic struct {
  15. models.BaseModel
  16. Title string `gorm:"type:varchar(255);not null;index;comment:标题"`
  17. Body string `gorm:"type:longtext;not null;comment:内容"`
  18. UserID uint `gorm:"type:bigint;not null; index;comment:用户ID"`
  19. CategoryID uint `gorm:"type:bigint;not null; index;comment:分类ID"`
  20. // 创建 user_id 和 category_id 外键关联
  21. User User
  22. Category Category
  23. models.CommonTimestampsField
  24. }
  25. up := func(migrator gorm.Migrator, DB *gorm.DB) {
  26. _ = DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci").AutoMigrate(&Topic{})
  27. }
  28. down := func(migrator gorm.Migrator, DB *gorm.DB) {
  29. _ = migrator.DropTable(&Topic{})
  30. }
  31. migrate.Add(up, down, "2025_07_16_155731_add_topics_table")
  32. }