{"record":{"id":"7fd4934880148d95","repo":"gastownhall/beads","slug":"query-s-constraint-violations-w","errorCode":null,"errorMessage":"query %s constraint violations: %w","messagePattern":"query (.+?) constraint violations: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/versioncontrolops/mergesettle.go","lineNumber":930,"sourceCode":"\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) {\n\t// table is from the fixed fkCascadeRepairDeletes allowlist, never user input.\n\t//nolint:gosec // G202: hardcoded table name.\n\trows, err := db.QueryContext(ctx,\n\t\t\"SELECT violation_type, violation_info FROM dolt_constraint_violations_\"+table)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"query %s constraint violations: %w\", table, err)\n\t}\n\tdefer rows.Close()\n\tfor rows.Next() {\n\t\tvar vtype string\n\t\tvar vinfo any\n\t\tif err := rows.Scan(&vtype, &vinfo); err != nil {\n\t\t\treturn false, fmt.Errorf(\"scan %s constraint violation: %w\", table, err)\n\t\t}\n\t\tif vtype != \"foreign key\" {\n\t\t\treturn false, nil\n\t\t}\n\t\t// Server mode returns violation_info as JSON text; the embedded engine\n\t\t// hands back the driver's native value (e.g. merge.FkCVMeta), which\n\t\t// marshals to the same JSON.\n\t\tvar infoJSON []byte\n\t\tswitch v := vinfo.(type) {\n\t\tcase []byte:\n\t\t\tinfoJSON = v","sourceCodeStart":912,"sourceCodeEnd":948,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/versioncontrolops/mergesettle.go#L912-L948","documentation":"violationsAreIssueFKOnly inspects the per-table dolt_constraint_violations_<table> rows to confirm all violations are foreign-key violations before auto-repair deletes anything. If the SELECT against that table fails, this wrapped error is returned and repair refuses to proceed (safety-first behavior).","triggerScenarios":"SELECT violation_type, violation_info FROM dolt_constraint_violations_<table> fails — the table doesn't exist because no violations were recorded, engine version mismatch on the per-table violations layout, or transaction/connection errors.","commonSituations":"Repair invoked when no violations table exists for that table (violations already resolved); Dolt versions naming or shaping the per-table violations table differently; server mode restricting system-table reads.","solutions":["Check the wrapped %w error: 'doesn't exist' typically means there are no violations for this table — safe to skip.","Confirm dolt_constraint_violations lists this table with num_violations > 0 before reading the per-table violations.","Retry once the merge transaction state is healthy.","Align Dolt engine version with what beads supports."],"exampleFix":"// before\nrows, err := db.QueryContext(ctx, \"SELECT violation_type, violation_info FROM dolt_constraint_violations_\"+table)\nif err != nil {\n    return false, fmt.Errorf(\"query %s constraint violations: %w\", table, err)\n}\n// after: treat missing violations table as no-violations\nrows, err := db.QueryContext(ctx, \"SELECT violation_type, violation_info FROM dolt_constraint_violations_\"+table)\nif err != nil {\n    if strings.Contains(err.Error(), \"doesn't exist\") {\n        return true, nil\n    }\n    return false, fmt.Errorf(\"query %s constraint violations: %w\", table, err)\n}","handlingStrategy":"validation","validationCode":"var n int\n_ = db.QueryRow(\"SELECT num_violations FROM dolt_constraint_violations WHERE `table` = ?\", table).Scan(&n)\n// only query per-table violations when n > 0","typeGuard":"func tableHasViolations(ctx context.Context, db DBConn, table string) bool {\n    var n int\n    _ = db.QueryRow(\"SELECT num_violations FROM dolt_constraint_violations WHERE `table` = ?\", table).Scan(&n)\n    return n > 0\n}","tryCatchPattern":"ok, err := violationsAreIssueFKOnly(ctx, db, t)\nif err != nil && strings.Contains(err.Error(), \"constraint violations\") {\n    if !tableHasViolations(ctx, db, t) {\n        continue // nothing to verify\n    }\n    return err\n}","preventionTips":["Confirm the violations summary row before reading per-table violations","Skip repair when the per-table violations table is absent","Align engine versions to keep violation_info encoding stable"],"tags":["dolt","foreign-key","constraint-violations"],"backgroundTag":"constraint-violations-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}