{"id":"9421144f376f9346","repo":"drizzle-team/drizzle-orm","slug":"cannot-execute-a-query-on-a-query-builder-please-942114","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":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/sqlite-core/query-builders/select.ts","lineNumber":913,"sourceCode":"> extends SQLiteSelectQueryBuilderBase<\n\tSQLiteSelectHKT,\n\tTTableName,\n\tTResultType,\n\tTRunResult,\n\tTSelection,\n\tTSelectMode,\n\tTNullabilityMap,\n\tTDynamic,\n\tTExcludedMethods,\n\tTResult,\n\tTSelectedFields\n> implements RunnableQuery<TResult, 'sqlite'>, SQLWrapper {\n\tstatic override readonly [entityKind]: string = 'SQLiteSelect';\n\n\t/** @internal */\n\t_prepare(isOneTimeQuery = true): SQLiteSelectPrepare<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<SQLiteColumn>(this.config.fields);\n\t\tconst query = this.session[isOneTimeQuery ? 'prepareOneTimeQuery' : 'prepareQuery'](\n\t\t\tthis.dialect.sqlToQuery(this.getSQL()),\n\t\t\tfieldsList,\n\t\t\t'all',\n\t\t\ttrue,\n\t\t\tundefined,\n\t\t\t{\n\t\t\t\ttype: 'select',\n\t\t\t\ttables: [...this.usedTables],\n\t\t\t},\n\t\t\tthis.cacheConfig,\n\t\t);\n\t\tquery.joinsNotNullableMap = this.joinsNotNullableMap;\n\t\treturn query as ReturnType<this['prepare']>;\n\t}\n","sourceCodeStart":895,"sourceCodeEnd":931,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/sqlite-core/query-builders/select.ts#L895-L931","documentation":"Thrown by SQLiteSelectBase._prepare (select.ts:913) when the select builder has no session attached. A select built via the standalone QueryBuilder (or otherwise detached from a db instance) cannot be executed because there is no driver connection to compile and run it against. You can still call .toSQL() on such a builder, but .run/.all/.get/.execute require a real db instance.","triggerScenarios":"Creating a query via new QueryBuilder() (the class, not db.$queryRaw) and calling .all()/.execute(); passing a select built outside a db context into something that calls .execute(); calling prepare()/run() on a builder returned by a helper that stripped the session.","commonSituations":"Unit-testing query construction without a db; extracting a query into a function that returns the builder instead of running it; using the wrong import (QueryBuilder vs db.select); SSR/edge runtimes where the db was not initialised before the call.","solutions":["Build the query from a db instance: db.select()... so the session is attached.","If you only need the SQL text, call .toSQL() instead of an execute method.","Ensure the db/client is initialised (e.g. drizzle(...)) before constructing runnable queries."],"exampleFix":"// before — QueryBuilder has no session\nconst qb = new QueryBuilder();\nconst rows = await qb.select().from(users).all(); // throws\n\n// after — build from a db instance\nconst db = drizzle(client);\nconst rows = await db.select().from(users).all();","handlingStrategy":"type-guard","validationCode":"function assertExecutable(builder) {\n  if (!builder.session) {\n    throw new Error('Builder has no db session; build from db.select() or use .toSQL()');\n  }\n}","typeGuard":"const isRunnable = (q): boolean =>\n  q && typeof q === 'object' && '_prepare' in q && !!q.session;","tryCatchPattern":null,"preventionTips":["Always construct runnable queries from a db instance, never from new QueryBuilder().","Use .toSQL() when you only need the SQL string (e.g. in tests).","Initialise the drizzle db before importing modules that build queries at module load."],"tags":["select","session","query-builder","runtime"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}