laravel/framework · error · RuntimeException

This database engine does not support lateral joins.

Error message

This database engine does not support lateral joins.

What it means

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.

Source

Thrown at src/Illuminate/Database/Query/Grammars/Grammar.php:219

            $joinWord = ($join->type === 'straight_join' && $this->supportsStraightJoins()) ? '' : ' join';

            return trim("{$join->type}{$joinWord} {$tableAndNestedJoins} {$this->compileWheres($join)}");
        })->implode(' ');
    }

    /**
     * Compile a "lateral join" clause.
     *
     * @param  \Illuminate\Database\Query\JoinLateralClause  $join
     * @param  string  $expression
     * @return string
     *
     * @throws \RuntimeException
     */
    public function compileJoinLateral(JoinLateralClause $join, string $expression): string
    {
        throw new RuntimeException('This database engine does not support lateral joins.');
    }

    /**
     * Determine if the grammar supports straight joins.
     *
     * @return bool
     *
     * @throws \RuntimeException
     */
    protected function supportsStraightJoins()
    {
        throw new RuntimeException('This database engine does not support straight joins.');
    }

    /**
     * Compile the "where" portions of the query.
     *
     * @param  \Illuminate\Database\Query\Builder  $query

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Run the query against a supported engine (pgsql, sqlsrv, or mysql >= 8.0.14).
  2. Rewrite the lateral join as a correlated subquery or use window functions that SQLite supports.
  3. Gate the lateral join behind a driver check and provide a portable alternative path for unsupported engines.

Example fix

// before
$query->joinLateral(function ($q) { ... }, 'latest');

// after (driver guard)
if (in_array($query->getConnection()->getDriverName(), ['pgsql','sqlsrv','mysql'])) {
    $query->joinLateral(function ($q) { ... }, 'latest');
} else {
    // portable equivalent without LATERAL
}
Defensive patterns

Strategy: validation

Validate before calling

$driver = $query->getConnection()->getDriverName();
if (in_array($driver, ['pgsql','sqlsrv','mysql'])) {
    $query->joinLateral($subquery, 'alias');
} else {
    // SQLite/MariaDB: use a portable correlated-subquery alternative
}

Type guard

function supportsLateralJoins(\Illuminate\Database\Connection $connection): bool
{
    if ($connection instanceof \Illuminate\Database\MySqlConnection) {
        return ! $connection->isMaria();
    }
    return $connection instanceof \Illuminate\Database\PostgresConnection
        || $connection instanceof \Illuminate\Database\SqlServerConnection;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/39ccece9d6b00a24.json. Report an issue: GitHub.