{"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":1281,"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":1263,"sourceCodeEnd":1299,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/bindings-typescript/src/server/runtime.ts#L1263-L1299","documentation":"For a multi-column unique index (including composite unique constraints), find() serializes its argument as a point key: you must pass exactly one value per indexed column, in index order. Passing a scalar, too few, or too many elements throws TypeError('wrong number of elements') before any datastore call is made.","triggerScenarios":"Calling find(scalarValue) against a 2-column unique index; find([a]) or find([a, b, c]) where the index covers exactly 2 columns; call sites not updated after the index definition changed arity.","commonSituations":"Index changed from single-column to composite (or vice versa) without updating queries; copy-paste from a table with a single-column index; stale generated bindings after a schema edit.","solutions":["Pass an array with exactly one value per indexed column, in index order: find([region, id])","Regenerate/refresh type information after changing index definitions so arity errors surface at compile time","Centralize each index lookup in one helper so the correct arity is written in a single place"],"exampleFix":"// before (unique index on [region, id])\nconst user = ctx.db.users.byRegionId.find(id); // scalar against 2-column index\n\n// after\nconst user = ctx.db.users.byRegionId.find([region, id]);","handlingStrategy":"type-guard","validationCode":"// before calling find on a composite unique index covering N columns:\nfunction assertIndexKey(colVal: unknown[], numColumns: number): void {\n  if (!Array.isArray(colVal) || colVal.length !== numColumns) {\n    throw new Error(`expected ${numColumns} key values, got ${Array.isArray(colVal) ? colVal.length : typeof colVal}`);\n  }\n}","typeGuard":"const hasIndexArity = (v: unknown, n: number): v is unknown[] =>\n  Array.isArray(v) && v.length === n;\n\n// usage:\nif (hasIndexArity(key, 2)) ctx.db.users.byRegionId.find(key as [typeof region, typeof id]);","tryCatchPattern":null,"preventionTips":["Regenerate bindings after any index change so the compiler enforces key arity","Keep one typed helper per composite index that constructs the key array in index order","When an index gains a column, grep all find/delete call sites for that index"],"tags":["index","database","query","typescript"],"backgroundTag":"invalid-index-query","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}