{"id":"d7f2d7a339aaa730","repo":"drizzle-team/drizzle-orm","slug":"your-f-path-join-field-references-a-col-d7f2d7","errorCode":null,"errorMessage":"Your \"${f.path.join('->')}\" field references a column \"${tableName}\".\"${f.field.name}\", but the table \"${tableName}\" is not part of the query! Did you forget to join it?","messagePattern":"Your \"(.+?)\" field references a column \"(.+?)\"\\.\"(.+?)\", but the table \"(.+?)\" is not part of the query! Did you forget to join it\\?","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/pg-core/dialect.ts","lineNumber":377,"sourceCode":"\t\tconst fieldsList = fieldsFlat ?? orderSelectedFields<PgColumn>(fields);\n\t\tfor (const f of fieldsList) {\n\t\t\tif (\n\t\t\t\tis(f.field, Column)\n\t\t\t\t&& getTableName(f.field.table)\n\t\t\t\t\t!== (is(table, Subquery)\n\t\t\t\t\t\t? table._.alias\n\t\t\t\t\t\t: is(table, PgViewBase)\n\t\t\t\t\t\t? table[ViewBaseConfig].name\n\t\t\t\t\t\t: is(table, SQL)\n\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t: getTableName(table))\n\t\t\t\t&& !((table) =>\n\t\t\t\t\tjoins?.some(({ alias }) =>\n\t\t\t\t\t\talias === (table[Table.Symbol.IsAlias] ? getTableName(table) : table[Table.Symbol.BaseName])\n\t\t\t\t\t))(f.field.table)\n\t\t\t) {\n\t\t\t\tconst tableName = getTableName(f.field.table);\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Your \"${\n\t\t\t\t\t\tf.path.join('->')\n\t\t\t\t\t}\" field references a column \"${tableName}\".\"${f.field.name}\", but the table \"${tableName}\" is not part of the query! Did you forget to join it?`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst isSingleTable = !joins || joins.length === 0;\n\n\t\tconst withSql = this.buildWithCTE(withList);\n\n\t\tlet distinctSql: SQL | undefined;\n\t\tif (distinct) {\n\t\t\tdistinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;\n\t\t}\n\n\t\tconst selection = this.buildSelection(fieldsList, { isSingleTable });\n","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/pg-core/dialect.ts#L359-L395","documentation":"An Error thrown by PgDialect.buildSelectQuery() (drizzle-orm/src/pg-core/dialect.ts:377) during query building. For each selected field, it checks that the field's column belongs either to the FROM table or to one of the joined aliases; if a selected column references a table that is neither the FROM table nor a join alias, it throws, telling you which field/path references which unjoined table.","triggerScenarios":"Calling db.select({ ... }) (or the relational query API) where a selected field references a column from a table that is not the FROM table and is not present in the joins array. Constructing the select throws before any SQL is sent to the DB.","commonSituations":"Selecting fields from a related table and forgetting the .leftJoin(...)/.innerJoin(...) for it; renaming a table so an existing join alias no longer matches; using a column from the wrong table instance (two different table singletons with the same name).","solutions":["Add the missing join for the named table: .leftJoin(table, eq(...)).","Verify the alias used in joins matches getTableName of the referenced column's table.","If selecting only from one table, make sure the field's column actually belongs to that table singleton.","Ensure you imported a single shared table definition rather than re-declaring it in two files."],"exampleFix":"// before — cities selected but never joined\ndb.select({ id: users.id, city: cities.name }).from(users);\n// Your \"city->...\" field references a column \"cities\".\"name\", but the table \"cities\" is not part of the query!\n\n// after — join the referenced table\ndb\n  .select({ id: users.id, city: cities.name })\n  .from(users)\n  .leftJoin(cities, eq(users.cityId, cities.id));","handlingStrategy":"validation","validationCode":"import { getTableName, is } from 'drizzle-orm';\nimport { Column } from 'drizzle-orm';\n\nfunction assertFieldsCovered(\n  fields: { field: any; path: string[] }[],\n  fromTable: any,\n  joinAliases: string[],\n): void {\n  const fromName = getTableName(fromTable);\n  for (const f of fields) {\n    if (is(f.field, Column)) {\n      const t = getTableName(f.field.table);\n      if (t !== fromName && !joinAliases.includes(t)) {\n        throw new Error(`Field ${f.path.join('->')} needs table ${t} joined`);\n      }\n    }\n  }\n}\n\n// call before/while building the select to fail fast with a clearer message","typeGuard":"import { is, getTableName } from 'drizzle-orm';\nimport { Column } from 'drizzle-orm';\n\nfunction allSelectedFieldsJoined(\n  fields: { field: any }[],\n  fromTable: any,\n  joins: { alias: string }[] | undefined,\n): boolean {\n  const allowed = new Set<string>([getTableName(fromTable)]);\n  for (const j of joins ?? []) allowed.add(j.alias);\n  return fields.every((f) => !is(f.field, Column) || allowed.has(getTableName(f.field.table)));\n}","tryCatchPattern":null,"preventionTips":["Whenever you select a field from a related table, add the matching join in the same edit.","Reuse a single shared table definition across files to avoid duplicate singletons that confuse the alias check.","Review select field lists during code review for orphaned cross-table references."],"tags":["postgres","query-builder","select","join","schema"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}