What happened?
Calendar is installed on a project using multiple Sites (not sure if that is a contributing factor or not). After a Calendar Event is initially saved, I'm attempting to add entries to one of several Matrix fields that result in an integrity constraint violation error:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`project`.`craft_elementactivity`, CONSTRAINT `craft_fk_fzzzjumymegobofvhctbieacxjdtkwwsowxq` FOREIGN KEY (`elementId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE) in /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Command.php:1320
This issue was initially reported to Craft here: craftcms/cms#16841
I followed up with Craft support through email with copies of composer.json, composer.lock, web log, and database backups and am happy to provide those as well.
Errors and Stack Trace (if available)
2025-03-06 06:45:22 [web.ERROR] [yii\db\IntegrityException] PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`project`.`craft_elementactivity`, CONSTRAINT `craft_fk_fzzzjumymegobofvhctbieacxjdtkwwsowxq` FOREIGN KEY (`elementId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE) in /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Command.php:1320
Stack trace:
#0 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Command.php(1320): PDOStatement->execute()
#1 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Command.php(1120): yii\db\Command->internalExecute('INSERT INTO `cr...')
#2 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/helpers/Db.php(1084): yii\db\Command->execute()
#3 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/services/Elements.php(2779): craft\helpers\Db::upsert('{{%elementactiv...', Array)
#4 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/controllers/ElementsController.php(1946): craft\services\Elements->trackActivity(Object(craft\elements\Entry), 'save')
#5 [internal function]: craft\controllers\ElementsController->actionApplyDraft()
#6 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#7 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#8 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('apply-draft', Array)
#9 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/web/Application.php(361): yii\base\Module->runAction('elements/apply-...', Array)
#10 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/web/Application.php(660): craft\web\Application->runAction('elements/apply-...', Array)
#11 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/web/Application.php(323): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#12 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#13 /Users/username/Repositories/project/craft/web/index.php(15): yii\base\Application->run()
#14 /Applications/Herd.app/Contents/Resources/valet/server.php(167): require('/Users/username...')
#15 {main}
Next yii\db\IntegrityException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`project`.`craft_elementactivity`, CONSTRAINT `craft_fk_fzzzjumymegobofvhctbieacxjdtkwwsowxq` FOREIGN KEY (`elementId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE)
The SQL being executed was: INSERT INTO `craft_elementactivity` (`elementId`, `userId`, `siteId`, `draftId`, `type`, `timestamp`) VALUES (3798, 1, 1, NULL, 'save', '2025-03-06 11:45:22') ON DUPLICATE KEY UPDATE `siteId`=VALUES(`siteId`), `draftId`=VALUES(`draftId`), `timestamp`=VALUES(`timestamp`) in /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Schema.php:676
Stack trace:
#0 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Command.php(1325): yii\db\Schema->convertException(Object(PDOException), 'INSERT INTO `cr...')
#1 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Command.php(1120): yii\db\Command->internalExecute('INSERT INTO `cr...')
#2 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/helpers/Db.php(1084): yii\db\Command->execute()
#3 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/services/Elements.php(2779): craft\helpers\Db::upsert('{{%elementactiv...', Array)
#4 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/controllers/ElementsController.php(1946): craft\services\Elements->trackActivity(Object(craft\elements\Entry), 'save')
#5 [internal function]: craft\controllers\ElementsController->actionApplyDraft()
#6 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#7 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#8 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('apply-draft', Array)
#9 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/web/Application.php(361): yii\base\Module->runAction('elements/apply-...', Array)
#10 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/web/Application.php(660): craft\web\Application->runAction('elements/apply-...', Array)
#11 /Users/username/Repositories/project/craft/vendor/craftcms/cms/src/web/Application.php(323): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#12 /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#13 /Users/username/Repositories/project/craft/web/index.php(15): yii\base\Application->run()
#14 /Applications/Herd.app/Contents/Resources/valet/server.php(167): require('/Users/username...')
#15 {main}
Additional Information:
Array
(
[0] => 23000
[1] => 1452
[2] => Cannot add or update a child row: a foreign key constraint fails (`project`.`craft_elementactivity`, CONSTRAINT `craft_fk_fzzzjumymegobofvhctbieacxjdtkwwsowxq` FOREIGN KEY (`elementId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE)
)
{"memory":11996232,"exception":"[object] (yii\\db\\IntegrityException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`project`.`craft_elementactivity`, CONSTRAINT `craft_fk_fzzzjumymegobofvhctbieacxjdtkwwsowxq` FOREIGN KEY (`elementId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE)
The SQL being executed was: INSERT INTO `craft_elementactivity` (`elementId`, `userId`, `siteId`, `draftId`, `type`, `timestamp`) VALUES (3798, 1, 1, NULL, 'save', '2025-03-06 11:45:22') ON DUPLICATE KEY UPDATE `siteId`=VALUES(`siteId`), `draftId`=VALUES(`draftId`), `timestamp`=VALUES(`timestamp`) at /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Schema.php:676)
[previous exception] [object] (PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`project`.`craft_elementactivity`, CONSTRAINT `craft_fk_fzzzjumymegobofvhctbieacxjdtkwwsowxq` FOREIGN KEY (`elementId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE) at /Users/username/Repositories/project/craft/vendor/yiisoft/yii2/db/Command.php:1320)"}
How can we reproduce this?
- Navigate to Calendar Events > Events
- Click New Event > Default
- Add only the Title and choose "Save and continue editing"
- Add new 'Text' block to Matrix field
- Enter any text in 'Text' field in slideout and click "Create entry"
Entry fails to save due to integrity constraint violation.
Calendar Edition
Pro
Calendar Version
5.0.16
Craft Version
5.6.13
When did this issue start?
Previous Calendar Version
No response
What happened?
Calendar is installed on a project using multiple Sites (not sure if that is a contributing factor or not). After a Calendar Event is initially saved, I'm attempting to add entries to one of several Matrix fields that result in an integrity constraint violation error:
This issue was initially reported to Craft here: craftcms/cms#16841
I followed up with Craft support through email with copies of composer.json, composer.lock, web log, and database backups and am happy to provide those as well.
Errors and Stack Trace (if available)
How can we reproduce this?
Entry fails to save due to integrity constraint violation.
Calendar Edition
Pro
Calendar Version
5.0.16
Craft Version
5.6.13
When did this issue start?
Previous Calendar Version
No response