tobi/qmd · error · Error

Collection '${collectionName}' not found

Error message

Collection '${collectionName}' not found

What it means

insertContext() looks up the collection by name in store_collections before writing context; if no row matches the given name it throws rather than inserting context for a nonexistent collection (which would be silently lost).

Source

Thrown at src/store.ts:3667

  // Rename in store_collections
  renameStoreCollection(db, oldName, newName);
}

// =============================================================================
// Context Management Operations
// =============================================================================

/**
 * Insert or update a context for a specific collection and path prefix.
 *
 * `store_collections` is keyed by name (`TEXT PRIMARY KEY`), not an integer id.
 * #754 retargeted this query from the dropped `collections` table but left
 * `WHERE id = ?`, which throws `no such column: id`.
 */
export function insertContext(db: Database, collectionName: string, pathPrefix: string, context: string): void {
  const coll = db.prepare(`SELECT name FROM store_collections WHERE name = ?`).get(collectionName) as { name: string } | null;
  if (!coll) {
    throw new Error(`Collection '${collectionName}' not found`);
  }

  updateStoreContext(db, coll.name, pathPrefix, context);
}

/**
 * Delete a context for a specific collection and path prefix.
 * Returns the number of contexts deleted.
 */
export function deleteContext(db: Database, collectionName: string, pathPrefix: string): number {
  // Remove context from store_collections
  const success = removeStoreContext(db, collectionName, pathPrefix);
  return success ? 1 : 0;
}

/**
 * Delete all global contexts (contexts with empty path_prefix).
 * Returns the number of contexts deleted.

View on GitHub (pinned to dbfd0b4736)

Solutions

  1. Create the collection first (qmd collection add) then add context
  2. Verify the name via `qmd collection list` / a SELECT before inserting
  3. Use the exact current name after renames

Example fix

// before
insertContext(db, 'mynote', '/', 'desc'); // collection is 'mynotes'
// after
const exists = db.prepare('SELECT 1 FROM store_collections WHERE name=?').get('mynote');
if (!exists) throw new Error('create collection first');
insertContext(db, 'mynote', '/', 'desc');
Defensive patterns

Strategy: validation

Validate before calling

const ok = db.prepare('SELECT 1 FROM store_collections WHERE name=?').get(name); if (ok) insertContext(db, name, prefix, ctx);

Type guard

const collectionExists = (db: Database, n: string) => db.prepare('SELECT 1 FROM store_collections WHERE name=?').get(n) != null;

Prevention

When it happens

Trigger: Calling insertContext(db, name, prefix, ctx) with a collection name that was never added (or was removed/renamed) — e.g. `qmd context add` before `qmd collection add`.

Common situations: Typos in collection names; running context commands after a collection rename; tests using in-memory DBs that never registered the collection.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of tobi/qmd@dbfd0b4736 (2026-08-28). Data as JSON: /api/errors/7756ad7f85d4ef12. Report an issue: GitHub.