refactor: BaseModel 主键列名统一 uid→id

- BaseModel 移除 gorm column:uid 和 json:uid 标签,统一为 id
- 更新 8 个 repository 文件中 28 处 raw SQL 列引用 (uid→id)
- 更新 3 个前端 JS 文件中 14 处 API 响应字段引用 (uid→id)
- 添加数据库迁移 SQL 脚本 (docs/migrations/001_uid_to_id.sql)
This commit is contained in:
2026-06-02 19:17:57 +08:00
parent 7fdea90ded
commit 5911cee01c
13 changed files with 70 additions and 43 deletions

View File

@ -26,7 +26,7 @@ func (r *CommentRepo) FindByID(id uint) (*model.Comment, error) {
var c model.Comment
err := r.db.Table("comments").
Select("comments.*, users.username as author_name, users.avatar as author_avatar").
Joins("LEFT JOIN users ON users.uid = comments.user_id").
Joins("LEFT JOIN users ON users.id = comments.user_id").
Where("comments.id = ?", id).
First(&c).Error
if err != nil {
@ -48,7 +48,7 @@ func (r *CommentRepo) FindRootComments(postID uint, offset, limit int) ([]model.
var list []model.Comment
err := base.
Select("comments.*, users.username as author_name, users.avatar as author_avatar").
Joins("LEFT JOIN users ON users.uid = comments.user_id").
Joins("LEFT JOIN users ON users.id = comments.user_id").
Order("comments.created_at DESC").
Offset(offset).Limit(limit).
Find(&list).Error
@ -76,7 +76,7 @@ func (r *CommentRepo) FindReplies(rootID uint) ([]model.Comment, error) {
var list []model.Comment
err := r.db.Table("comments").
Select("comments.*, users.username as author_name, users.avatar as author_avatar").
Joins("LEFT JOIN users ON users.uid = comments.user_id").
Joins("LEFT JOIN users ON users.id = comments.user_id").
Where("comments.root_id = ? AND comments.parent_id IS NOT NULL AND comments.is_deleted = false", rootID).
Order("comments.created_at ASC").
Find(&list).Error
@ -101,8 +101,8 @@ func (r *CommentRepo) fillReplyToNames(list []model.Comment) {
}
var rows []nameRow
r.db.Table("users").
Select("uid, username").
Where("uid IN (SELECT reply_to_uid FROM comments WHERE root_id IN (?) AND parent_id IS NOT NULL)", r.rootIDsFromList(list)).
Select("id, username").
Where("id IN (SELECT reply_to_uid FROM comments WHERE root_id IN (?) AND parent_id IS NOT NULL)", r.rootIDsFromList(list)).
Find(&rows)
m := make(map[string]string)