singlestore-labs / singlestoredb-laravel-driver

The official SingleStore Laravel driver.
https://github.com/singlestore-labs/singlestore-laravel-driver
Apache License 2.0
224 stars 22 forks source link

Issue with User-Defined Variables in Eloquent ORM Queries after Updating to Laravel 11 #86

Closed Nelwhix closed 1 day ago

Nelwhix commented 3 weeks ago

Hi SingleStore Team,

I have been having a great experience using the SingleStore Laravel driver across several projects. However, after updating to Laravel 11, I started encountering an error when performing find or first queries using the Eloquent ORM. Interestingly, the error does not occur with get queries.

Here is the error message I am receiving:

SQLSTATE[HY000]: General error: 2508 Unknown user-defined variable @`laravel_row` being used before being set. A user defined variable's value can be set via running `select <value> into @<variable name>`. Ensure that this variable is set by running 'select @`laravel_row`'. Also note that user-defined variables are scoped to the session in which they're declared, much like temporary tables. (Connection: singlestore, SQL: select `laravel_table`.*, @laravel_row := if(@laravel_group = `pivot_user_id`, @laravel_row + 1, 1) as `laravel_row`, @laravel_group := `pivot_user_id` from (select @laravel_row := 0, @laravel_group := 0) as `laravel_vars`, (select `workspaces`.*, `user_workspace`.`user_id` as `pivot_user_id`, `user_workspace`.`workspace_id` as `pivot_workspace_id`, `user_workspace`.`created_at` as `pivot_created_at`, `user_workspace`.`updated_at` as `pivot_updated_at` from `workspaces` inner join `user_workspace` on `workspaces`.`id` = `user_workspace`.`workspace_id` where `user_workspace`.`user_id` = 01j00tmy116nw7rqyqp16jt11t and `workspaces`.`id` = 01j00tz9dae8613v4dnz5ctcdg and `workspaces`.`deleted_at` is null order by `pivot_user_id` asc) as `laravel_table` having `laravel_row` <= 1 order by `laravel_row`)

Query:

   $workspace = auth()->user()->workspaces()->find($workspaceId);

This issue occurs on both SingleStore Cloud and the SingleStoreDB Dev Image. I am using the SingleStore Laravel driver version 1.5.4.

Environment Details:

Laravel version: 11.13.0 PHP version: 8.2.19 SingleStore version: 8.5.17 (both cloud and dev image) I suspect the issue might be related to how user-defined variables are handled in the generated query. Could you please investigate this and provide guidance or a fix?

Thank you for your assistance!

JustSteveKing commented 3 weeks ago

Also watching this 👀

AdalbertMemSQL commented 3 weeks ago

Hey Thanks for reporting the issue. I'm investigating it at the moment. Looks like Laravel 11 has groupLimit functionality which generates queries unsupported by SingleStore. Will try to fix this

AdalbertMemSQL commented 3 weeks ago

This should be fixed by https://github.com/singlestore-labs/singlestoredb-laravel-driver/pull/87 I created a new release (v1.5.5) which contains this fix.

AdalbertMemSQL commented 3 weeks ago

@Nelwhix @JustSteveKing Can you check and confirm, if it works?

JustSteveKing commented 3 weeks ago

@Nelwhix @JustSteveKing Can you check and confirm, if it works?

Will let you know, thanks for the quick turn around!

bhushan commented 3 weeks ago

Thank you @AdalbertMemSQL 🎊