Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLSTATE[42000]: Syntax error or access violation: 1055 'website.stories.user_id' isn't in GROUP BY #145

Open
therezor opened this issue Feb 6, 2024 · 3 comments

Comments

@therezor
Copy link

therezor commented Feb 6, 2024

Looks like queries are not compatible with SQL strict mode.

Simple code will cause an issue: SQLSTATE[42000]: Syntax error or access violation: 1055 'website.stories.user_id' isn't in GROUP BY

 Story::published()->withAllTags($this->tag)->count()
@therezor
Copy link
Author

therezor commented Feb 6, 2024

Query that was produced by withAllTags:

SELECT
  count(*) AS aggregate
FROM
  (
    SELECT
      `stories`.*
    FROM
      `stories`
      INNER JOIN `taggables` AS `taggables_scopewithalltags_1` ON `stories`.`id` = `taggables_scopewithalltags_1`.`taggable_id`
      AND `taggables_scopewithalltags_1`.`taggable_type` = App \ Models \ Story
    WHERE
      `published_at` IS NOT NULL
      AND `published_at` <= 2024 -02 -06 11: 34: 35
      AND `taggables_scopewithalltags_1`.`tag_id` IN (1)
      AND `stories`.`deleted_at` IS NULL
    GROUP BY
      `stories`.`id`
    HAVING
      COUNT(DISTINCT taggables_scopewithalltags_1.tag_id) = 1
  ) AS `temp_table`

@cviebrock
Copy link
Owner

Yeah, this is a bit of a known issue. The fix (as far as I understand it to be), would be to change the code so that the generated SQL is changed from:

SELECT `stories`.*

to:

SELECT `stories`.`column1`, `stories`.`column2`, `stories`.`column3`,  ...

i.e. it needs to iterate and list all the columns from that table. That's pretty awkward, IMO.

If you can think of a better solution, I'm totally open to implementing it!

@therezor
Copy link
Author

@cviebrock what do you thin about using subquery instead of join (whereHas)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants