{"id":"40fb1365dac0bafe","repo":"drizzle-team/drizzle-orm","slug":"your-f-path-join-field-references-a-col-40fb13","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":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-orm/src/mysql-core/dialect.ts","lineNumber":353,"sourceCode":"\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, MySqlViewBase)\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(\n\t\t\t\t\t\t({ alias }) =>\n\t\t\t\t\t\t\talias\n\t\t\t\t\t\t\t\t=== (table[Table.Symbol.IsAlias]\n\t\t\t\t\t\t\t\t\t? getTableName(table)\n\t\t\t\t\t\t\t\t\t: 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\t\t'->',\n\t\t\t\t\t\t)\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\tconst distinctSql = distinct ? sql` distinct` : undefined;\n\n\t\tconst selection = this.buildSelection(fieldsList, { isSingleTable });\n\n\t\tconst tableSql = (() => {","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-orm/src/mysql-core/dialect.ts#L335-L371","documentation":"Thrown by MySqlDialect.buildSelectQuery (line 353) when a selected field references a column whose table is neither the main FROM table nor present in any join. Drizzle validates field-to-table provenance at SQL build time to prevent generating invalid cross-table references.","triggerScenarios":"In db.select({ x: otherTable.col }).from(mainTable) without a join on otherTable; or selecting a column from a table alias that was never joined. The loop over fieldsList detects the orphan column.","commonSituations":"Forgetting to add .leftJoin/.innerJoin for a table whose column you reference in select/where; copy-paste from another query that included a join; referencing a column by the wrong table reference after aliasing.","solutions":["Add the missing join: .leftJoin(otherTable, eq(mainTable.id, otherTable.mainId)).","If you only need the column from the other table, ensure that table is in the FROM/JOIN list.","Re-check that the column reference (e.g. users.id) belongs to a table actually present in the query.","Use aliased tables consistently - don't reference the original table name if you aliased it."],"exampleFix":"// before\ndb.select({ name: users.name, petName: pets.name })\n  .from(users); // pets not joined\n\n// after\ndb.select({ name: users.name, petName: pets.name })\n  .from(users)\n  .leftJoin(pets, eq(users.id, pets.ownerId));","handlingStrategy":"validation","validationCode":"// Before building, ensure every referenced table is joined\nconst joinedAliases = new Set([mainTableName, ...joins.map((j) => j.alias)]);\nfor (const col of selectedColumns) {\n  if (!joinedAliases.has(getTableName(col.table))) {\n    throw new Error(`Missing join for table ${getTableName(col.table)}`);\n  }\n}","typeGuard":"function isColumnInQuery(col: MySqlColumn, fromTable: string, joins: { alias: string }[]): boolean {\n  const t = getTableName(col.table);\n  return t === fromTable || joins.some((j) => j.alias === t);\n}","tryCatchPattern":null,"preventionTips":["Add a join for every table whose column you reference in select/where.","Use consistent aliasing - reference the alias, not the original, when aliased.","Code-review queries that reference multiple tables for missing joins."],"tags":["mysql","select","joins","query-builder","schema"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}