flipped-aurora/gin-vue-admin · error

查询自动代码重复记录失败: %w

Error message

查询自动代码重复记录失败: %w

What it means

Thrown by autoCodeIdentityExists when the COUNT query that detects an existing auto-code record (same struct/abbreviation/package) fails. The function only returns (true) when count>0; a query error is wrapped so Create can abort instead of silently generating duplicate code.

Source

Thrown at server/service/system/auto_code_persistence.go:165

}

func autoCodeIdentityExists(ctx context.Context, db *gorm.DB, info request.AutoCode) (bool, error) {
	if db == nil {
		return false, errors.New("自动代码数据库未初始化")
	}
	var count int64
	err := db.WithContext(ctx).Model(&model.SysAutoCodeHistory{}).
		Where(
			"business_db = ? AND (struct_name = ? OR abbreviation = ?) AND package = ? AND flag = ?",
			info.BusinessDB,
			info.StructName,
			info.Abbreviation,
			info.Package,
			0,
		).
		Count(&count).Error
	if err != nil {
		return false, fmt.Errorf("查询自动代码重复记录失败: %w", err)
	}
	return count > 0, nil
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Inspect the wrapped %w cause and fix the underlying driver error.
  2. Run migrations to ensure the auto-code history table exists.
  3. Verify DB connectivity/config before running code generation.
Defensive patterns

Strategy: try-catch

Validate before calling

if err := db.Exec("SELECT 1").Error; err != nil {
    return fmt.Errorf("database unreachable: %w", err)
}

Try / catch

exists, err := autoCodeIdentityExists(ctx, db, info)
if err != nil {
    log.Printf("duplicate check failed: %v", errors.Unwrap(err))
    return err // do NOT treat as "not duplicate"
}

Prevention

When it happens

Trigger: The GORM Count on the auto-code history/records table errors during Create or the TestAutoCodeIdentityExistsReturnsQueryError test path: missing table, broken DB connection, cancelled context, or SQL syntax error from schema drift.

Common situations: Fresh database without migrations for the auto-code history table; DB outage mid-request; tests injecting a failing DB to verify error propagation; wrong datasource in config.

Related errors


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