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

SQL error related to GROUP BY #19

Closed
icreatestuff opened this issue Feb 24, 2020 · 21 comments
Closed

SQL error related to GROUP BY #19

icreatestuff opened this issue Feb 24, 2020 · 21 comments

Comments

@icreatestuff
Copy link

icreatestuff commented Feb 24, 2020

Hello,

I'm getting the following SQL error

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test-site.elements_sites.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

The stack trace seems to indicate it's originating from line 103 in the Similar.php service which is
$results = $query->all();. I suspect it's something in the setup of that $query but couldn't see anywhere that GROUP By was being used.

In case it helps the full query that's been compiled is

The SQL being executed was: SELECT `elements`.`id`, `elements_sites`.`siteId`, COUNT(*) AS `count`
FROM (SELECT `elements`.`id` AS `elementsId`, `elements_sites`.`id` AS `elementsSitesId`, `content`.`id` AS `contentId`
FROM `elements` `elements`
INNER JOIN `entries` `entries` ON `entries`.`id` = `elements`.`id`
INNER JOIN `elements_sites` `elements_sites` ON `elements_sites`.`elementId` = `elements`.`id`
INNER JOIN `content` `content` ON `content`.`elementId` = `elements`.`id`
INNER JOIN `structureelements` `structureelements` ON (`structureelements`.`elementId` = `elements`.`id`) AND (`structureelements`.`structureId`=9)
LEFT JOIN `relations` ON elements.id=`relations`.sourceId
WHERE (`entries`.`sectionId`='15') AND ((elements.id != '2782') AND (`relations`.`targetId` IN ('2777', '2778'))) AND (`elements`.`archived`=FALSE) AND (((`elements`.`enabled`=TRUE) AND (`elements_sites`.`enabled`=TRUE)) AND (`entries`.`postDate` <= '2020-02-24 18:01:00') AND ((`entries`.`expiryDate` IS NULL) OR (`entries`.`expiryDate` > '2020-02-24 18:01:00'))) AND (`elements`.`dateDeleted` IS NULL) AND (`elements_sites`.`enabled`=TRUE) AND (`elements`.`draftId` IS NULL) AND (`elements`.`revisionId` IS NULL)
GROUP BY `elements`.`id`, `structureelements`.`structureId`, `structureelements`.`lft`
ORDER BY `structureelements`.`lft`, `entries`.`postDate` DESC) `subquery`
INNER JOIN `entries` `entries` ON `entries`.`id` = `subquery`.`elementsId`
INNER JOIN `elements` `elements` ON `elements`.`id` = `subquery`.`elementsId`
INNER JOIN `elements_sites` `elements_sites` ON `elements_sites`.`id` = `subquery`.`elementsSitesId`
INNER JOIN `content` `content` ON `content`.`id` = `subquery`.`contentId`
INNER JOIN `structureelements` `structureelements` ON (`structureelements`.`elementId` = `subquery`.`elementsId`) AND (`structureelements`.`structureId`=9)
LEFT JOIN `relations` ON elements.id=`relations`.sourceId
WHERE `relations`.`targetId` IN ('2777', '2778')
GROUP BY `relations`.`sourceId`, `elements`.`id`
ORDER BY `count` DESC
LIMIT 4

which is being built from this twig code

{% set ids = entry.tags.ids() %}
{% set limitCriteria = craft.entries.section('news').with(['coverImage', 'person']).limit(4) %}
{% set similarEntries = craft.similar.find({ element: entry, context: ids, criteria: limitCriteria }) %}

Craft v3.4.8
Similar v1.0.6
MySQL 5.7.26
PHP 7.2.20

@khalwat
Copy link
Contributor

khalwat commented Feb 27, 2020

mmmm not sure what the issue is here; PRs would be welcome!

@goodmixer
Copy link

Having same issue locally. Disabling strict mode on MySQL got rid of the error but not always possible on shared hosting environments.

@robmcfadden
Copy link

Having issue as well. Haven't had time to dig into the Craft CMS changelog, but Similar broke right after upgrading to the latest. Was working just before upgrading to the latest. I can post more details if needed, but a bit busy at the moment.

@robmcfadden
Copy link

Figured out how to work around my problem.

Was getting... Column not found: 1054 Unknown column 'structureelements.structureId' in 'group statement'

And had this code...

{% set ids = entry.productCategories.ids()|merge(entry.learnCategories.ids()) %}
{% set limitCriteria = craft.entries.section('articles').limit(4) %}
{% set relatedArticles = craft.similar.find({ element: entry, context: ids, criteria: limitCriteria }) %}

Removing the section parameter from limitCriteria got rid of the error. I can make this work for my needs. I'm realizing now that this wasn't actually the same error that was reported here. Sorry if I bloated the comments unnecessarily. :/

@icreatestuff
Copy link
Author

@robmcfadden No worries! I actually have a very similar setup in the template so tried also removing the section parameter but it had no affect, I still got the error.

@khalwat It's a bit over my head to be honest, is there anything that needs flagging up with the Craft dev team on this?

@jangidgirish
Copy link

Same issue with me.

@khalwat
Copy link
Contributor

khalwat commented Mar 13, 2020

Happy to have a PR to fix the issue!

@icreatestuff
Copy link
Author

Just a quick update to say this remains an issue in Craft v3.4.10.1

Interestingly on MySQL 5.6.47 my code is also broken unless I remove the .section parameter (as @robmcfadden suggested). On MySQL 5.7.26 it's broken with or without that parameter.

@brianlarson
Copy link

brianlarson commented Mar 26, 2020

I just ran into this as well after updating from Craft 3.3 to Craft 3.4. I'd PR if my brain was bigger but it's a pea. Happening locally with MySQL v5.7.22.

{% set ids = entry.blogCategories.ids()|merge(entry.blogTags.ids()) %}
{% set entries = craft.similar.find({ 
  element: entry, 
  context: ids, 
  criteria: craft.entries.limit(4)
}) ?? null %}

@agrigg
Copy link

agrigg commented Mar 26, 2020

I'm getting the same error as @robmcfadden and removing the section parameter fixed it for me as well.

@khalwat
Copy link
Contributor

khalwat commented Apr 16, 2020

I've been unusually busy lately, and this doesn't look like a simple problem to solve. Would love it if anyone from the community is able to help out to diagnose and fix this.

@klick
Copy link

klick commented May 5, 2020

Had the same error and changing the section parameter from .section('section') to .sectionId(2) solved it for me.

@brianlarson
Copy link

Sill having this issue on Similar v1.0.6 but it looks like in the changelog it was fixed? MySQL v5.7.22, Craft 3.4.20. I'd be down this being a paid plugin fer sure! 👍

@khalwat
Copy link
Contributor

khalwat commented May 19, 2020

Still accepting PRs! @brianlarson have you tried what @klick mentioned above?

@brianlarson
Copy link

I did but unfortunately it didn't werk in my case!

@joshuabaker
Copy link

A combination of running the good ol’ SET SESSION sql_mode trick (below via a module) and setting the sectionId instead of section seems to work for us.

try {
    Craft::$app
        ->getDb()
        ->createCommand('SET SESSION sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";')
        ->execute();
} catch (Exception $exception) {
    // do nothing
}

There might be an official Craft way of doing this since initSql was removed.

@khalwat
Copy link
Contributor

khalwat commented Feb 25, 2021

I'm going to see about devoting some time to fixing this up in the near term

@khalwat
Copy link
Contributor

khalwat commented Mar 3, 2021

This should be fixed in the dev-develop branch

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-similar": "dev-develop as 1.1.0”,

Then do a composer update

Let me know how you go...

@brettburwell
Copy link

@khalwat Really appreciate you taking a look at this. I can confirm that the update corrected the issue on my end.

@khalwat
Copy link
Contributor

khalwat commented Mar 3, 2021

Thanks, will let a few others hopefully verify before releasing... anyone able to test on Postgres?

@khalwat khalwat closed this as completed in aa230ba Mar 5, 2021
@khalwat
Copy link
Contributor

khalwat commented Mar 5, 2021

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

No branches or pull requests

10 participants