{"record":{"id":"80f3a3655f1e991b","repo":"laravel/framework","slug":"this-database-engine-does-not-support-lateral-join-80f3a3","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/MariaDbGrammar.php","lineNumber":22,"sourceCode":"\nuse Illuminate\\Database\\Query\\Builder;\nuse Illuminate\\Database\\Query\\JoinLateralClause;\nuse RuntimeException;\n\nclass MariaDbGrammar extends MySqlGrammar\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     * Compile a \"JSON value cast\" statement into SQL.\n     *\n     * @param  string  $value\n     * @return string\n     */\n    public function compileJsonValueCast($value)\n    {\n        return \"json_query({$value}, '$')\";\n    }\n\n    /**\n     * Compile a query to get the number of open connections for a database.\n     *\n     * @return string\n     */","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php#L4-L40","documentation":"MariaDbGrammar explicitly overrides compileJoinLateral to throw, even though it extends MySqlGrammar (which implements lateral joins). MariaDB does not support LATERAL joins as of its current stable releases, so the framework enforces this limitation at compile time rather than letting invalid SQL reach the server.","triggerScenarios":"Calling joinSubLateral() or leftJoinSubLateral() on a query builder whose connection uses MariaDbGrammar (configured as 'mariadb' driver). For example: DB::table('users')->joinSubLateral($sub, 'o', 'o.user_id', 'users.id')->get().","commonSituations":"Using MariaDB as the primary database while developing features that use lateral joins (which work on MySQL 8+). Confusion between MySQL and MariaDB feature sets — lateral joins are MySQL-only and explicitly blocked on MariaDB.","solutions":["Switch to MySQL 8.0.14+ (change DB_CONNECTION/driver from mariadb to mysql) which supports lateral joins","Refactor the lateral join into a correlated subquery in a regular joinSub() — functionally equivalent without LATERAL","Use a window function or CTE approach to avoid needing a lateral join altogether","Switch to PostgreSQL which also supports lateral joins"],"exampleFix":"// before — fails on MariaDB\nDB::table('users')\n    ->joinSubLateral(\n        DB::table('orders')->select('user_id', DB::raw('SUM(total) as spent'))->groupBy('user_id'),\n        'o', 'o.user_id', 'users.id'\n    )->get();\n\n// after — joinSub works on MariaDB (no LATERAL needed here)\nDB::table('users')\n    ->joinSub(\n        DB::table('orders')->select('user_id', DB::raw('SUM(total) as spent'))->groupBy('user_id'),\n        'o', 'o.user_id', 'users.id'\n    )->get();","handlingStrategy":"type-guard","validationCode":"$conn = DB::connection();\n$isMaria = $conn instanceof \\Illuminate\\Database\\MariaDbConnection;\n\nif (! $isMaria) {\n    $query->joinSubLateral($sub, 'o', 'o.user_id', 'users.id');\n} else {\n    // MariaDB lacks LATERAL — use regular joinSub\n    $query->joinSub($sub, 'o', 'o.user_id', 'users.id');\n}","typeGuard":"function supportsLateralJoins(): bool {\n    $conn = DB::connection();\n    $isMaria = $conn instanceof \\Illuminate\\Database\\MariaDbConnection;\n    $isSupportedDriver = in_array($conn->getDriverName(), ['mysql', 'pgsql', 'sqlsrv']);\n    return $isSupportedDriver && ! $isMaria;\n}","tryCatchPattern":"try {\n    $query->joinSubLateral($sub, 'o', 'o.user_id', 'users.id');\n} catch (\\RuntimeException $e) {\n    if (str_contains($e->getMessage(), 'lateral joins')) {\n        $query->joinSub($sub, 'o', 'o.user_id', 'users.id');\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Check whether the connection is MariaDbConnection before using lateral joins","Use joinSub() as the portable default — it works on MariaDB and all other engines","Document MariaDB-specific limitations in project docs when choosing MariaDB over MySQL"],"tags":["joins","lateral","mariadb","database","grammar"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}