{"id":"39ccece9d6b00a24","repo":"laravel/framework","slug":"this-database-engine-does-not-support-lateral-join","errorCode":null,"errorMessage":"This database engine does not support lateral joins.","messagePattern":"This database engine does not support lateral joins\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Grammars/Grammar.php","lineNumber":219,"sourceCode":"\n            $joinWord = ($join->type === 'straight_join' && $this->supportsStraightJoins()) ? '' : ' join';\n\n            return trim(\"{$join->type}{$joinWord} {$tableAndNestedJoins} {$this->compileWheres($join)}\");\n        })->implode(' ');\n    }\n\n    /**\n     * Compile a \"lateral join\" clause.\n     *\n     * @param  \\Illuminate\\Database\\Query\\JoinLateralClause  $join\n     * @param  string  $expression\n     * @return string\n     *\n     * @throws \\RuntimeException\n     */\n    public function compileJoinLateral(JoinLateralClause $join, string $expression): string\n    {\n        throw new RuntimeException('This database engine does not support lateral joins.');\n    }\n\n    /**\n     * Determine if the grammar supports straight joins.\n     *\n     * @return bool\n     *\n     * @throws \\RuntimeException\n     */\n    protected function supportsStraightJoins()\n    {\n        throw new RuntimeException('This database engine does not support straight joins.');\n    }\n\n    /**\n     * Compile the \"where\" portions of the query.\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Grammars/Grammar.php#L201-L237","documentation":"The base Grammar::compileJoinLateral() throws because lateral joins are not implemented for this engine. LATERAL joins allow a subquery in the JOIN clause to reference columns of preceding tables; only Postgres, SQL Server, and MySQL 8.0.14+ override this method. SQLite and any grammar that does not override compileJoinLateral falls through to this RuntimeException.","triggerScenarios":"Calling joinLateral($subquery, $alias) or leftJoinLateral($subquery, $alias) while the connection driver is sqlite (or any custom grammar that extends the base Grammar without overriding compileJoinLateral).","commonSituations":"Using a lateral join (common for per-row top-N / windowed subquery patterns) in code that also runs against SQLite in tests. Deploying lateral-join query code to an environment whose DB_CONNECTION was changed to sqlite.","solutions":["Run the query against a supported engine (pgsql, sqlsrv, or mysql >= 8.0.14).","Rewrite the lateral join as a correlated subquery or use window functions that SQLite supports.","Gate the lateral join behind a driver check and provide a portable alternative path for unsupported engines."],"exampleFix":"// before\n$query->joinLateral(function ($q) { ... }, 'latest');\n\n// after (driver guard)\nif (in_array($query->getConnection()->getDriverName(), ['pgsql','sqlsrv','mysql'])) {\n    $query->joinLateral(function ($q) { ... }, 'latest');\n} else {\n    // portable equivalent without LATERAL\n}","handlingStrategy":"validation","validationCode":"$driver = $query->getConnection()->getDriverName();\nif (in_array($driver, ['pgsql','sqlsrv','mysql'])) {\n    $query->joinLateral($subquery, 'alias');\n} else {\n    // SQLite/MariaDB: use a portable correlated-subquery alternative\n}","typeGuard":"function supportsLateralJoins(\\Illuminate\\Database\\Connection $connection): bool\n{\n    if ($connection instanceof \\Illuminate\\Database\\MySqlConnection) {\n        return ! $connection->isMaria();\n    }\n    return $connection instanceof \\Illuminate\\Database\\PostgresConnection\n        || $connection instanceof \\Illuminate\\Database\\SqlServerConnection;\n}","tryCatchPattern":null,"preventionTips":["Track which engines support LATERAL when writing portable query code.","Add a driver test matrix for any query that uses joinLateral.","Prefer window-function alternatives that are more widely supported."],"tags":["lateral-join","database-engine","sqlite","subquery"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}