get_primary_keys.go 306 B

1234567891011121314151617
  1. package utils
  2. import (
  3. "gorm.io/gorm"
  4. )
  5. func GetPrimaryKeys(obj interface{}, db *gorm.DB) (result []string, err error) {
  6. stmt := &gorm.Statement{DB: db}
  7. if err = stmt.Parse(obj); err != nil {
  8. return
  9. }
  10. for _, v := range stmt.Schema.PrimaryFields {
  11. result = append(result, v.DBName)
  12. }
  13. return
  14. }