{"record":{"id":"cc5d8fef86399a09","repo":"gastownhall/beads","slug":"query-constraint-violations-w-cc5d8f","errorCode":null,"errorMessage":"query constraint violations: %w","messagePattern":"query constraint violations: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/versioncontrolops/mergesettle.go","lineNumber":906,"sourceCode":"\t// deletes above did not cover the constraint that fired, and committing\n\t// would persist a violated working set.\n\tremaining, err := constraintViolationTables(ctx, db)\n\tif err != nil {\n\t\treturn false, true, err\n\t}\n\tif len(remaining) > 0 {\n\t\treturn false, true, nil\n\t}\n\treturn true, true, nil\n}\n\n// constraintViolationTables lists the tables with outstanding constraint\n// violations in the working set.\nfunc constraintViolationTables(ctx context.Context, db DBConn) ([]string, error) {\n\trows, err := db.QueryContext(ctx,\n\t\t\"SELECT `table` FROM dolt_constraint_violations WHERE num_violations > 0\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"query constraint violations: %w\", err)\n\t}\n\tdefer rows.Close()\n\tvar tables []string\n\tfor rows.Next() {\n\t\tvar t string\n\t\tif err := rows.Scan(&t); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"scan constraint violation: %w\", err)\n\t\t}\n\t\ttables = append(tables, t)\n\t}\n\treturn tables, rows.Err()\n}\n\n// violationsAreIssueFKOnly reports whether every constraint violation recorded\n// for table is a foreign-key violation referencing issues — the only class the\n// cascade repair understands. violation_info is Dolt's JSON descriptor; its\n// ReferencedTable names the FK's parent.\nfunc violationsAreIssueFKOnly(ctx context.Context, db DBConn, table string) (bool, error) {","sourceCodeStart":888,"sourceCodeEnd":924,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/versioncontrolops/mergesettle.go#L888-L924","documentation":"constraintViolationTables enumerates tables with outstanding constraint violations by querying dolt_constraint_violations. If the query itself fails, this error is returned. It typically means the violations system table is unavailable or the query is rejected by the engine.","triggerScenarios":"SELECT `table` FROM dolt_constraint_violations WHERE num_violations > 0 fails — table missing (old Dolt version or no merge has run), server mode rejects the system-table query, or connection/transaction errors.","commonSituations":"Dolt version lacking the dolt_constraint_violations table; querying outside a session where merge metadata is loaded; server connection dropped.","solutions":["Check the wrapped %w error: 'table doesn't exist' means no violations or unsupported engine version.","Verify Dolt version supports constraint violation tracking (dolt_constraint_violations).","Retry after the connection/transaction is healthy; constraint violations only exist after a merge with FK issues.","If no merge is in progress, treat absence of the table as 'no violations'."],"exampleFix":"// before\nrows, err := db.QueryContext(ctx, \"SELECT `table` FROM dolt_constraint_violations WHERE num_violations > 0\")\nif err != nil {\n    return nil, fmt.Errorf(\"query constraint violations: %w\", err)\n}\n// after: tolerate missing table as zero violations\nrows, err := db.QueryContext(ctx, \"SELECT `table` FROM dolt_constraint_violations WHERE num_violations > 0\")\nif err != nil {\n    if strings.Contains(err.Error(), \"doesn't exist\") {\n        return nil, nil\n    }\n    return nil, fmt.Errorf(\"query constraint violations: %w\", err)\n}","handlingStrategy":"type-guard","validationCode":"var hasTable int\n_ = db.QueryRow(\"SELECT COUNT(*) FROM information_schema.tables WHERE table_name='dolt_constraint_violations'\").Scan(&hasTable)\n// hasTable == 0 means no violation tracking / no violations","typeGuard":"func violationsTableAvailable(db DBConn) bool {\n    var n int\n    _ = db.QueryRow(\"SELECT COUNT(*) FROM information_schema.tables WHERE table_name='dolt_constraint_violations'\").Scan(&n)\n    return n > 0\n}","tryCatchPattern":"tables, err := constraintViolationTables(ctx, db)\nif err != nil && strings.Contains(err.Error(), \"query constraint violations\") {\n    if !violationsTableAvailable(db) {\n        return nil // no violations possible\n    }\n    return err\n}","preventionTips":["Only inspect violations right after a conflicting merge","Check engine support for dolt_constraint_violations before relying on it","Handle missing-table as the no-violations case"],"tags":["dolt","constraint-violations","sql-query"],"backgroundTag":"constraint-violations-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}