{"record":{"id":"6dd124c5fbafea93","repo":"clockworklabs/SpacetimeDB","slug":"wrong-number-of-elements","errorCode":null,"errorMessage":"wrong number of elements","messagePattern":"wrong number of elements","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/bindings-typescript/src/server/runtime.ts","lineNumber":1286,"sourceCode":"          BINARY_WRITER.reset(buf);\n          serializeRow(BINARY_WRITER, row);\n          sys.datastore_update_bsatn(\n            table_id,\n            index_id,\n            buf.buffer,\n            BINARY_WRITER.offset\n          );\n          integrateGeneratedColumns?.(row, buf.view);\n          return row;\n        };\n      }\n      index = base as UniqueIndex<any, any>;\n    } else if (isUnique) {\n      // numColumns != 1, unique index\n      const base = {\n        find: (colVal: IndexVal<any, any>): RowType<any> | null => {\n          if (colVal.length !== numColumns) {\n            throw new TypeError('wrong number of elements');\n          }\n          const buf = LEAF_BUF;\n          const point_len = serializePoint(buf, colVal);\n          const iter_id = sys.datastore_index_scan_point_bsatn(\n            index_id,\n            buf.buffer,\n            point_len\n          );\n          return tableIterateOne(iter_id, deserializeRow);\n        },\n        delete: (colVal: IndexVal<any, any>): boolean => {\n          if (colVal.length !== numColumns)\n            throw new TypeError('wrong number of elements');\n\n          const buf = LEAF_BUF;\n          const point_len = serializePoint(buf, colVal);\n          const num = sys.datastore_delete_by_index_scan_point_bsatn(\n            index_id,","sourceCodeStart":1268,"sourceCodeEnd":1304,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/bindings-typescript/src/server/runtime.ts#L1268-L1304","documentation":"For a UNIQUE index spanning multiple columns, the runtime builds a `find` that takes an array of column values and checks `colVal.length !== numColumns` before serializing the point lookup. Passing fewer or more elements than the index has columns throws `wrong number of elements`. This is the multi-column counterpart of a single-value lookup; the array must match the index's column list exactly, in definition order.","triggerScenarios":"Calling `table.<multiColUniqueIndex>.find([...])` with the wrong array length: `idx.find([1])` on a two-column unique index, or `idx.find([1, 2, 3])` on a two-column index; reordering code after adding/removing a column from the index without updating call sites.","commonSituations":"Adding a column to a composite unique index (e.g. `(tenantId, email)` -> `(tenantId, email, region)`) and forgetting one lookup site; copy-pasting a `find` from a single-column index and not extending the array; conditional lookups that sometimes omit trailing columns.","solutions":["Pass exactly one value per indexed column, in the same order the columns are declared in the index: for an index on `[a, b]` call `idx.find([aVal, bVal])`.","After editing an index definition, grep every `find`/`delete` on that index and update the arrays.","If you only know a prefix, that is a range scan, not a point lookup — use `iter`/range APIs instead of `find`.","Add a length assertion helper in your code that ties the array to the index columns at development time."],"exampleFix":"// before — index on (tenantId, email)\nconst user = byTenantEmail.find([tenantId]); // TypeError: wrong number of elements\n\n// after\nconst user = byTenantEmail.find([tenantId, email]);","handlingStrategy":"validation","validationCode":"// Centralize composite-key construction next to the index definition.\nconst TENANT_EMAIL_COLS = 2; // index on (tenantId, email)\nfunction tenantEmailKey(tenantId: bigint, email: string): unknown[] {\n  const key = [tenantId, email];\n  if (key.length !== TENANT_EMAIL_COLS) throw new Error('bad key arity');\n  return key;\n}\n// byTenantEmail.find(tenantEmailKey(tenantId, email));","typeGuard":"type CompositeKey2<A, B> = [A, B]; // tuple types fix arity at compile time\nfunction isCompositeKey2<A, B>(k: unknown[]): k is CompositeKey2<A, B> { return k.length === 2; }","tryCatchPattern":"try { const row = idx.find(key); } catch (e) { if (e instanceof TypeError && e.message === 'wrong number of elements') { /* log expected arity vs provided length, fall back to full scan + filter */ } else throw e; }","preventionTips":["Type lookup keys as tuples whose length equals the index column count.","Never spread dynamic arrays directly into find(); construct them in one helper.","When an index definition changes, grep for its name and re-check every call site's array length."],"tags":["index","unique-constraint","query","runtime","spacetimedb"],"backgroundTag":"index-lookup-arity-mismatch","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}