{"id":"824690a1fe42c89d","repo":"drizzle-team/drizzle-orm","slug":"cannot-execute-a-query-on-a-query-builder-please-824690","errorCode":null,"errorMessage":"Cannot execute a query on a query builder. Please use a database instance instead.","messagePattern":"Cannot execute a query on a query builder\\. Please use a database instance instead\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/singlestore-core/query-builders/select.ts","lineNumber":992,"sourceCode":"\tTResult = SelectResult<TSelection, TSelectMode, TNullabilityMap>[],\n\tTSelectedFields = BuildSubquerySelection<TSelection, TNullabilityMap>,\n> extends SingleStoreSelectQueryBuilderBase<\n\tSingleStoreSelectHKT,\n\tTTableName,\n\tTSelection,\n\tTSelectMode,\n\tTPreparedQueryHKT,\n\tTNullabilityMap,\n\tTDynamic,\n\tTExcludedMethods,\n\tTResult,\n\tTSelectedFields\n> {\n\tstatic override readonly [entityKind]: string = 'SingleStoreSelect';\n\n\tprepare(): SingleStoreSelectPrepare<this> {\n\t\tif (!this.session) {\n\t\t\tthrow new Error('Cannot execute a query on a query builder. Please use a database instance instead.');\n\t\t}\n\t\tconst fieldsList = orderSelectedFields<SingleStoreColumn>(this.config.fields);\n\t\tconst query = this.session.prepareQuery<\n\t\t\tSingleStorePreparedQueryConfig & { execute: SelectResult<TSelection, TSelectMode, TNullabilityMap>[] },\n\t\t\tTPreparedQueryHKT\n\t\t>(this.dialect.sqlToQuery(this.getSQL()), fieldsList, undefined, undefined, undefined, {\n\t\t\ttype: 'select',\n\t\t\ttables: [...this.usedTables],\n\t\t}, this.cacheConfig);\n\t\tquery.joinsNotNullableMap = this.joinsNotNullableMap;\n\t\treturn query as SingleStoreSelectPrepare<this>;\n\t}\n\n\t$withCache(config?: { config?: CacheConfig; tag?: string; autoInvalidate?: boolean } | false) {\n\t\tthis.cacheConfig = config === undefined\n\t\t\t? { config: {}, enable: true, autoInvalidate: true }\n\t\t\t: config === false\n\t\t\t? { enable: false }","sourceCodeStart":974,"sourceCodeEnd":1010,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/singlestore-core/query-builders/select.ts#L974-L1010","documentation":"Thrown in SingleStoreSelectBase.prepare (select.ts:992) when this.session is undefined. A select built via the query-builder factory ($drizzle.createQueryBuilder() / SingleStoreSelectBuilder without a session) has no database connection and therefore cannot be prepared or executed; only a SingleStoreDatabase instance (db.select(...)) carries a session.","triggerScenarios":"Calling .execute(), .prepare(), or .iterator() on a select created via createQueryBuilder (the 'qb' builder mode) instead of via the db instance; passing a builder around and trying to run it directly.","commonSituations":"Using drizzle's query-builder API to compose SQL for inspection but accidentally calling execute on it; refactoring db access into a layer that receives a qb instead of db; confusing the query-builder (composition only) with the database (execution).","solutions":["Use the database instance to build executable queries: `db.select(...).from(...)` instead of `qb.select(...)`.","If you only need the SQL string from a builder, call .toSQL() (which does not require a session).","Pass the db (or tx) into the function that needs to execute, not the bare query builder."],"exampleFix":"// before\nconst qb = db.$with('qb');\nconst q = db.select({id: users.id}).from(users); // built from qb w/o session\nawait q.execute();\n// after\nconst q = db.select({id: users.id}).from(users);\nawait q.execute();","handlingStrategy":"type-guard","validationCode":"function isExecutable(select) {\n  return Boolean(select.session);\n}\nif (!isExecutable(q)) throw new Error('Use db.select(), not a session-less query builder, to execute');","typeGuard":"function isRunnableSelect(q): boolean {\n  return Boolean(q && q.session);\n}","tryCatchPattern":null,"preventionTips":["Build executable selects from the db (or tx) instance, not createQueryBuilder.","Use .toSQL() when you only need SQL text from a builder.","Pass db/tx into functions that need to execute queries."],"tags":["query-builder","session","singlestore","execution"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}