{"id":"2d915d757e2d9d33","repo":"drizzle-team/drizzle-orm","slug":"no-fields-selected-for-table-tableconfig-tsname-2d915d","errorCode":null,"errorMessage":"No fields selected for table \"${tableConfig.tsName}\" (\"${tableAlias}\")","messagePattern":"No fields selected for table \"(.+?)\" \\(\"(.+?)\"\\)","errorType":"validation","errorClass":"DrizzleError","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/pg-core/dialect.ts","lineNumber":1348,"sourceCode":"\t\t\t\t\ton: sql`true`,\n\t\t\t\t\ttable: new Subquery(builtRelation.sql as SQL, {}, relationTableAlias),\n\t\t\t\t\talias: relationTableAlias,\n\t\t\t\t\tjoinType: 'left',\n\t\t\t\t\tlateral: true,\n\t\t\t\t});\n\t\t\t\tselection.push({\n\t\t\t\t\tdbKey: selectedRelationTsKey,\n\t\t\t\t\ttsKey: selectedRelationTsKey,\n\t\t\t\t\tfield,\n\t\t\t\t\trelationTableTsKey: relationTableTsName,\n\t\t\t\t\tisJson: true,\n\t\t\t\t\tselection: builtRelation.selection,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (selection.length === 0) {\n\t\t\tthrow new DrizzleError({ message: `No fields selected for table \"${tableConfig.tsName}\" (\"${tableAlias}\")` });\n\t\t}\n\n\t\tlet result;\n\n\t\twhere = and(joinOn, where);\n\n\t\tif (nestedQueryRelation) {\n\t\t\tlet field = sql`json_build_array(${\n\t\t\t\tsql.join(\n\t\t\t\t\tselection.map(({ field, tsKey, isJson }) =>\n\t\t\t\t\t\tisJson\n\t\t\t\t\t\t\t? sql`${sql.identifier(`${tableAlias}_${tsKey}`)}.${sql.identifier('data')}`\n\t\t\t\t\t\t\t: is(field, SQL.Aliased)\n\t\t\t\t\t\t\t? field.sql\n\t\t\t\t\t\t\t: field\n\t\t\t\t\t),\n\t\t\t\t\tsql`, `,\n\t\t\t\t)","sourceCodeStart":1330,"sourceCodeEnd":1366,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/pg-core/dialect.ts#L1330-L1366","documentation":"Thrown while building a nested relational query (db.query.X.findMany/findFirst with `with`/`columns` clauses) when the field selection for a given table resolves to zero columns. The relational mapper at dialect.ts:1347 requires every joined/selected table to contribute at least one field; an empty selection would produce invalid JSON building SQL. The names in the message are the TS table name and its alias.","triggerScenarios":"Using the RQB relational API and filtering a nested relation's columns down to nothing — e.g. `db.query.users.findMany({ with: { posts: { columns: {} } } })`, or applying a `where`/`extras` combination that strips all columns. Also possible when a relation's referenced table has no selectable columns after a partial-column filter.","commonSituations":"Dynamically building a `columns` whitelist from user input and ending up with an empty object; migrating a findFirst config and accidentally leaving an empty `columns: {}`; misusing `with` on a relation while trying to fetch only an aggregate/count via `extras`.","solutions":["Ensure every table/relation in the relational query selects at least one column (omit `columns` to select all, or list at least one).","If the column list is dynamic, fall back to selecting the primary key or all columns when the computed list is empty.","Review nested `with` blocks and `columns` filters for accidental empty objects.","If you only need an aggregate, select it through `extras` alongside a real column or restructure the query."],"exampleFix":"// before\nawait db.query.users.findMany({\n  with: { posts: { columns: {} } }, // empty -> error\n});\n\n// after\nawait db.query.users.findMany({\n  with: { posts: { columns: { id: true } } },\n});","handlingStrategy":"validation","validationCode":"// Ensure every relation in a relational query selects >= 1 column.\nfunction assertColumnsSelected(config: Record<string, any>) {\n  for (const [rel, cfg] of Object.entries(config.with ?? {})) {\n    if (cfg && 'columns' in cfg && cfg.columns !== undefined\n        && Object.keys(cfg.columns).length === 0) {\n      throw new Error(`Relation \"${rel}\" has no columns selected`);\n    }\n    if (cfg?.with) assertColumnsSelected(cfg);\n  }\n}\n// run before db.query.X.findMany(config)","typeGuard":"function hasColumnsObject(cfg: unknown): cfg is { columns: Record<string, boolean> } {\n  return typeof cfg === 'object' && cfg !== null\n    && 'columns' in cfg && typeof (cfg as any).columns === 'object';\n}","tryCatchPattern":null,"preventionTips":["Omit `columns` to select all rather than passing an empty object.","When building column whitelists from user input, default to the primary key if the set is empty.","Review nested `with` configs after refactors."],"tags":["relational-query","rqb","selection","validation"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}