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

[5.x]: Saving element in afterSave results in integrity constraint violation exception #15517

Closed
timkelty opened this issue Aug 10, 2024 · 5 comments
Labels

Comments

@timkelty
Copy link
Contributor

timkelty commented Aug 10, 2024

What happened?

Description

If you need to (re)-save an element in its afterSave, you will get an integrity constraint violation on the elements_sites table, unless you manually include isNewForSite = false.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '17-1' for key 'elements_sites.idx_yuhsxkwkfvwtibgopgvkaghhoafftioctgde'
The SQL being executed was: INSERT INTO `elements_sites` (`elementId`, `siteId`, `title`, `slug`, `uri`, `enabled`, `content`, `dateCreated`, `dateUpdated`, `uid`) VALUES (17, 1, 'Entry #17', 'foo', 'blog/foo', 1, NULL, '2024-08-10 20:12:30', '2024-08-10 20:12:30', '8db0279e-b40a-4727-b352-cf5db88f64f4')

This only happens when trying to save something outside of the normal CP entry form, so it may have something to do with saving directly to a live element vs starting as a draft. Craft 4 has no problem with the same code.

Here's a gist of a contrived example where you want to put the entry ID in the title, so you re-save the entry in the afterSave event: https://gist.github.com/timkelty/1cfb09a9611587552ed0f81ab99b1af7

Craft CMS version

5.3

Related Issues

https://github.com/craftcms/craftnet/pull/1098

@timkelty timkelty added the bug label Aug 10, 2024
@steverowling
Copy link
Contributor

steverowling commented Aug 12, 2024

The problem also manifests for me on Craft 4.11.0.2, when using the afterPropagate event handler.

Adding the event->sender->isNewForSite = false; line from the gist resolves the issue.

This only appeared to start happening after updating to Craft 4.11.0.2 from 4.10.8. Perhaps related to this item in the 4.11.0 changelog:

Fixed a bug where $element->isNewForSite was always false from fields’ normalizeValue() methods when propagating an element to a new site.

For reference, my site is not multisite, if that makes any difference.

@i-just
Copy link
Contributor

i-just commented Aug 12, 2024

@steverowling, thanks for the additional info! We’re looking into it.

@timkelty
Copy link
Contributor Author

ah thanks @steverowling! I must have been testing an earlier version of 4.x

@mckenzieclark
Copy link

@brandonkelly thanks for addressing! Could this fix also be added to 4.x?

@brandonkelly
Copy link
Member

Craft 4.11.2 and 5.3.3 are out with a fix for this!

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

No branches or pull requests

5 participants