19 lines
535 B
Go
19 lines
535 B
Go
|
|
package seeder
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// isTableEmpty 检查给定模型对应的数据库表是否为空。
|
||
|
|
// 注意:此函数需要从 database 包中移动过来,或者在 seeder 包中重新定义,
|
||
|
|
// 为了避免循环依赖,这里选择在 seeder 包中重新定义。
|
||
|
|
func isTableEmpty(tx *gorm.DB, model interface{}) (bool, error) {
|
||
|
|
var count int64
|
||
|
|
if err := tx.Model(model).Count(&count).Error; err != nil {
|
||
|
|
return false, fmt.Errorf("查询表记录数失败: %w", err)
|
||
|
|
}
|
||
|
|
return count == 0, nil
|
||
|
|
}
|