gastownhall/beads · error

dropping dependencies' existing primary key for migration 00

Error message

dropping dependencies' existing primary key for migration 0053: %w

What it means

Wraps failure of `ALTER TABLE dependencies DROP PRIMARY KEY`. The drifted #4690 shape may key dependencies by some other column; because a table has only one PRIMARY KEY, the existing one must be removed before id can become it (uk_dep_* unique keys from 0043 already enforce real uniqueness).

Source

Thrown at internal/storage/schema/migration_repairs.go:549

		return nil
	}

	if _, err := db.ExecContext(ctx, "ALTER TABLE dependencies MODIFY COLUMN id CHAR(36) NOT NULL"); err != nil {
		return fmt.Errorf("finalizing dependencies.id for migration 0053: %w", err)
	}

	hasAnyPrimaryKey, err := schemaHasPrimaryKey(ctx, db, "dependencies")
	if err != nil {
		return fmt.Errorf("checking dependencies for an existing primary key: %w", err)
	}
	if hasAnyPrimaryKey {
		// The #4690 drifted shape has dependencies keyed some other way (or
		// keyless): a table can carry only one PRIMARY KEY, so whatever is
		// there must go before id can become it. The uk_dep_* natural-identity
		// unique keys (0043) enforce the real uniqueness independently of
		// whatever this was, so dropping it is safe.
		if _, err := db.ExecContext(ctx, "ALTER TABLE dependencies DROP PRIMARY KEY"); err != nil {
			return fmt.Errorf("dropping dependencies' existing primary key for migration 0053: %w", err)
		}
	}
	if _, err := db.ExecContext(ctx, "ALTER TABLE dependencies ADD PRIMARY KEY (id)"); err != nil {
		return fmt.Errorf("keying dependencies.id for migration 0053: %w", err)
	}
	return nil
}

// firstNonNullString returns the first valid (non-NULL) value among cols, or
// "" if all are NULL.
func firstNonNullString(cols ...sql.NullString) string {
	for _, c := range cols {
		if c.Valid {
			return c.String
		}
	}
	return ""
}

View on GitHub (pinned to 71377f2769)

Solutions

  1. Close all other connections to the database and rerun `bd`
  2. Confirm the account has ALTER/INDEX privileges: GRANT ALTER, INDEX ON beads.* TO ...
  3. Check for foreign keys referencing the old PK and drop/recreate them around the repair
  4. Inspect the wrapped %w error for the server-side reason (lock, FK, privilege) and address it

Example fix

// before: missing ALTER privilege
GRANT ALTER, INDEX ON beads.* TO 'beads'@'localhost';
// after: rerun migration
bd ready
Defensive patterns

Strategy: validation

Validate before calling

-- confirm no foreign keys reference the current PK before upgrading
SELECT TABLE_NAME, CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = 'dependencies';
-- confirm ALTER privilege
SHOW GRANTS FOR CURRENT_USER();

Try / catch

if err := ensureSchema(ctx, db); err != nil {
    if strings.Contains(err.Error(), "dropping dependencies' existing primary key") {
        if strings.Contains(err.Error(), "denied") { /* grant ALTER/INDEX */ }
        if strings.Contains(err.Error(), "foreign key") { /* drop/recreate FKs */ }
        return retryAfterRemediation(err)
    }
    return err
}

Prevention

When it happens

Trigger: The table has a non-id PRIMARY KEY and `ALTER TABLE dependencies DROP PRIMARY KEY` fails: table locked by a concurrent writer, foreign keys referencing the current PK (some engines refuse), insufficient privileges (ALTER/INDEX), or Dolt rejecting the operation mid-repair.

Common situations: Another bd or SQL session open during upgrade; database user missing ALTER privilege; schema drift from out-of-band Dolt SQL edits that added referencing foreign keys.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/015f47f69186d04b. Report an issue: GitHub.