different-ai/openwork · error

Unable to backfill ${repair.table}.organization_id; orphan i

Error message

Unable to backfill ${repair.table}.organization_id; orphan ids: ${orphanIds.join(", ")}

What it means

Error "Unable to backfill ${repair.table}.organization_id; orphan ids: ${orphanIds.join(", ")}" thrown in different-ai/openwork.

Source

Thrown at ee/packages/den-db/src/schema-repairs.ts:137

    `UPDATE ${table} child_table
     JOIN ${parentTable} parent_table ON child_table.${foreignKey} = parent_table.\`id\`
     SET child_table.\`organization_id\` = parent_table.\`organization_id\`
     WHERE child_table.\`organization_id\` IS NULL`,
  )
  console.log(`[den-db] ${repair.table}.organization_id backfilled from ${repair.parentTable}`)

  const nullRows = await executor.query(`SELECT COUNT(*) AS null_count FROM ${table} WHERE \`organization_id\` IS NULL`)
  if (numericValue(nullRows, "null_count") > 0) {
    const orphanRows = await executor.query(
      `SELECT child_table.\`id\` AS id FROM ${table} child_table
       LEFT JOIN ${parentTable} parent_table ON child_table.${foreignKey} = parent_table.\`id\`
       WHERE child_table.\`organization_id\` IS NULL
       LIMIT 20`,
    )
    const orphanIds = orphanRows
      .map((row) => row.id)
      .filter((id): id is string => typeof id === "string")
    throw new Error(
      `Unable to backfill ${repair.table}.organization_id; orphan ids: ${orphanIds.join(", ")}`,
    )
  }

  await runDdl(executor, `ALTER TABLE ${table} MODIFY \`organization_id\` varchar(64) NOT NULL`)
  console.log(`[den-db] ${repair.table}.organization_id made NOT NULL`)
}

async function repairOrganizationColumn(executor: Executor, repair: OrganizationRepair) {
  const table = quoteIdentifier(repair.table)

  const countRows = await executor.query(`SELECT COUNT(*) AS row_count FROM ${table}`)
  if (numericValue(countRows, "row_count") === 0) {
    await runDdl(executor, `ALTER TABLE ${table} ADD COLUMN \`organization_id\` varchar(64) NOT NULL AFTER \`id\``)
    console.log(`[den-db] ${repair.table}.organization_id column added`)
    return
  }

View on GitHub (pinned to 2b7df46e8a)

When it happens

Trigger: Thrown at ee/packages/den-db/src/schema-repairs.ts:137 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/33960ff8065af5b1. Report an issue: GitHub.