cviebrock / eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
MIT License
537 stars 72 forks source link

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

Open therezor opened 7 months ago

therezor commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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