bytebase/bytebase · error

failed to get keys

Error message

failed to get keys

What it means

Wraps an error returned by getKeys while getKeyAndIndexes assembles the full index/key metadata map for a MSSQL database. getKeys reads key/PK/unique index rows from catalog views; the failure means that query or its iteration failed.

Source

Thrown at backend/plugin/db/mssql/sync.go:1423

		}
		slices.SortFunc(tableIndexes[k], func(a, b *storepb.IndexMetadata) int {
			if a.Name < b.Name {
				return -1
			}
			if a.Name > b.Name {
				return 1
			}
			return 0
		})
	}
	return tableIndexes, nil
}

// getIndexes gets all indices of a database.
func getKeyAndIndexes(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.IndexMetadata, error) {
	keys, err := getKeys(txn, schemas)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to get keys")
	}
	indexes, err := getIndexes(txn, schemas)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to get indexes")
	}
	for k, v := range indexes {
		keys[k] = append(keys[k], v...)
	}
	return keys, nil
}

// getViews gets all views of a database.
func getViews(txn *sql.Tx, columnMap map[db.TableKey][]*storepb.ColumnMetadata) (map[string][]*storepb.ViewMetadata, error) {
	viewMap := make(map[string][]*storepb.ViewMetadata)

	// Get view comments separately
	viewCommentsMap := make(map[int]string)
	commentsQuery := `

View on GitHub (pinned to 1870550677)

Solutions

  1. Unwrap the inner error to identify the failing catalog query, then fix that root cause.
  2. Grant VIEW DEFINITION and catalog view access to the sync login.
  3. Retry on transient errors (deadlock/timeout); reduce scope by syncing specific schemas.
Defensive patterns

Strategy: try-catch

Validate before calling

// confirm the sync login can read key metadata before calling sync
const probe = "SELECT TOP 1 1 FROM sys.key_constraints"
if _, err := txn.Query(probe); err != nil {
    return fmt.Errorf("insufficient catalog permissions for getKeys: %w", err)
}

Try / catch

keys, err := getKeys(txn, schemas)
if err != nil {
    return nil, fmt.Errorf("failed to get keys: %w — check permissions on sys.key_constraints and retry", err)
}

Prevention

When it happens

Trigger: Calling syncDatabases (MSSQL) when the getKeys catalog query against sys.indexes/sys.key_constraints fails or its row iteration errors.

Common situations: Permission-restricted sync logins; huge catalogs timing out; deadlocks with concurrent schema changes; broken connections during sync.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/642d56b51ee39449. Report an issue: GitHub.