flipped-aurora/gin-vue-admin · error

岗位ID不能为空

Error message

岗位ID不能为空

What it means

UpdateSysPosition validates that the position struct carries a non-zero ID before issuing the GORM update. A zero ID means the update target is unspecified, so the service returns this error instead of performing an update that would match no rows.

Source

Thrown at server/service/system/sys_position.go:25

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"gorm.io/gorm"
)

type SysPositionService struct{}

var SysPositionServiceApp = new(SysPositionService)

// CreateSysPosition 创建岗位
func (s *SysPositionService) CreateSysPosition(ctx context.Context, pos *system.SysPosition) (err error) {
	return global.GVA_DB.WithContext(ctx).Create(pos).Error
}

// UpdateSysPosition 更新岗位
func (s *SysPositionService) UpdateSysPosition(ctx context.Context, pos *system.SysPosition) (err error) {
	if pos.ID == 0 {
		return errors.New("岗位ID不能为空")
	}
	return global.GVA_DB.WithContext(ctx).Model(&system.SysPosition{}).Where("id = ?", pos.ID).Updates(map[string]interface{}{
		"name":   pos.Name,
		"code":   pos.Code,
		"sort":   pos.Sort,
		"status": pos.Status,
		"remark": pos.Remark,
	}).Error
}

// DeleteSysPosition 删除岗位, 已有用户绑定时禁止删除
func (s *SysPositionService) DeleteSysPosition(ctx context.Context, id uint) (err error) {
	if id == 0 {
		return errors.New("岗位ID不能为空")
	}
	var joinCount int64
	if err = global.GVA_DB.WithContext(ctx).Model(&system.SysUserPosition{}).Where("sys_position_id = ?", id).Count(&joinCount).Error; err != nil {
		return err

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Ensure the request body includes the position's numeric id before calling the update API.
  2. Check frontend form binding so the row's id is carried into the payload.
  3. In Go code, fetch the position first (GetSysPosition) and set pos.ID before updating.

Example fix

// before
positionApi.updatePosition({ name: 'Engineer', code: 'ENG' }) // no id
// after
positionApi.updatePosition({ ID: 3, name: 'Engineer', code: 'ENG' })
Defensive patterns

Strategy: validation

Validate before calling

if (!form.ID || form.ID <= 0) {
  alert('缺少岗位ID,无法更新')
  return
}
await positionApi.updatePosition(form)

Type guard

function hasValidPositionId(pos) {
  return typeof pos?.ID === 'number' && pos.ID > 0
}

Try / catch

try {
  await positionApi.updatePosition(form)
} catch (e) {
  if (e.msg === '岗位ID不能为空') {
    alert('请先保存或选择一个已有岗位再修改')
  } else throw e
}

Prevention

When it happens

Trigger: Calling PUT /position/updatePosition (SysPositionService.UpdateSysPosition) with a body whose id field is missing or 0 — e.g. frontend submits a newly created (unsaved) row or omits id from the payload.

Common situations: Frontend edit dialog losing the row's id after refresh; JSON field name mismatch (id vs ID) so the field deserializes to 0; constructing SysPosition in code without setting ID.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/9320553b25641374. Report an issue: GitHub.