Expected "(" on EF-generated query

2

The following code is an SQL query that's being run on a MySQL Server 5.7:

SELECT
  `t`.`c`,
  `t`.`Id`,
  `t0`.`Id`,
  `t0`.`Bio`,
  `t0`.`Image`,
  `t0`.`Name`,
  `t0`.`Follower_id`,
  `t0`.`Followed_id`
FROM
  (
    SELECT
      (
        SELECT
          COUNT(*)
        FROM
          `PROFILE_FOLLOW_PROFILE` AS `p`
        WHERE
          `u`.`Id` = `p`.`Followed_id`
      ) AS `c`,
      `u`.`Id`
    FROM
      `USER_PROFILES` AS `u`
    WHERE
      `u`.`Id` = 2
    LIMIT
      1
  ) AS `t`
  LEFT JOIN LATERAL (
    SELECT
      `u0`.`Id`,
      `u0`.`Bio`,
      `u0`.`Image`,
      `u0`.`Name`,
      `p0`.`Follower_id`,
      `p0`.`Followed_id`
    FROM
      `PROFILE_FOLLOW_PROFILE` AS `p0`
      INNER JOIN `USER_PROFILES` AS `u0` ON `p0`.`Follower_id` = `u0`.`Id`
    WHERE
      `t`.`Id` = `p0`.`Followed_id`
    ORDER BY
      (
        SELECT
          1
      )
    LIMIT
      10 OFFSET 0
  ) AS `t0` ON TRUE
ORDER BY
  `t`.`Id`,
  `t0`.`Follower_id`,
  `t0`.`Followed_id`,
  `t0`.`Id`

This query won't run and will return the following error:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(     SELECT       `u0`.`Id`,       `u0`.`Bio`,       `u0`.`Image`,       `u0`.`' at line 29

This statement is generated from the following EF Query:

        var selectedProfiles = _context.USER_PROFILES
                                    .Where(x => x.Id == id)
                                    .Select(x => new PagedListResult<ProfileForPostDto>
                                    {
                                        Total = x.Followers.Count,
                                        Data = (x.Followers
                                            .Select(f => new ProfileForPostDto
                                            {
                                                Id = f.Follower.Id,
                                                Bio = f.Follower.Bio,
                                                Image = f.Follower.Image,
                                                Name = f.Follower.Name
                                            })
                                            .Skip((pageNum - 1) * pageSize)
                                            .Take(pageSize)
                                            .ToList()),
                                        Page = pageNum,
                                        PageSize = pageSize
                                    }).FirstOrDefault();

I've been checking the syntax of the sql query and it seems to be correct according to myself and SQL validators.

The log on the program points to the same part of the statement:

2020-03-02 20:06:44.239 +01:00 [Error] Failed executing DbCommand ("9"ms) [Parameters=["@__id_0='?' (DbType = Int32), @__pageSize_3='?' (DbType = Int32), @__p_2='?' (DbType = Int32)"], CommandType='Text', CommandTimeout='30']"
""SELECT `t`.`Id`, `t1`.`Id`, `t1`.`Bio`, `t1`.`Image`, `t1`.`Name`, `t1`.`Type`, `t1`.`User`, `t1`.`Follower_id`, `t1`.`Followed_id`
FROM (
    SELECT `u`.`Id`
    FROM `USER_PROFILES` AS `u`
    WHERE `u`.`Id` = @__id_0
    LIMIT 1
) AS `t`
LEFT JOIN LATERAL (
    SELECT `u0`.`Id`, `u0`.`Bio`, `u0`.`Image`, `u0`.`Name`, `u0`.`Type`, `u0`.`User`, `t`.`Follower_id`, `t`.`Followed_id`
    FROM (
        SELECT `p`.`Follower_id`, `p`.`Followed_id`
        FROM `PROFILE_FOLLOW_PROFILE` AS `p`
        WHERE `t`.`Id` = `p`.`Followed_id`
        ORDER BY (SELECT 1)
        LIMIT @__pageSize_3 OFFSET @__p_2
    ) AS `t0`
    INNER JOIN `USER_PROFILES` AS `u0` ON `t`.`Follower_id` = `u0`.`Id`
) AS `t1` ON TRUE
ORDER BY `t`.`Id`, `t1`.`Follower_id`, `t1`.`Followed_id`, `t1`.`Id`"
2020-03-02 20:06:44.308 +01:00 [Error] An exception occurred while iterating over the results of a query for context type '"Cosprea.EntityFrameworkCore.Models.CospreaAPIContext"'."
""MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(
    SELECT `u0`.`Id`, `u0`.`Bio`, `u0`.`Image`, `u0`.`Name`, `u0`.`Type`, `u0' at line 8

I can't figure out what's wrong, it doesn't make any sense to add another "(" where it says.

Any idea?

c#
mysql
sql
entity-framework
asked on Stack Overflow Mar 3, 2020 by Alex Coronas

1 Answer

3

MySQL doesn't support lateral Join

answered on Stack Overflow Mar 3, 2020 by Alex Coronas

User contributions licensed under CC BY-SA 3.0