Skip to content

Integrity constraint violation when saving Matrix fields in Calendar event #379

@jrrdnx

Description

@jrrdnx

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?

  1. Navigate to Calendar Events > Events
  2. Click New Event > Default
  3. Add only the Title and choose "Save and continue editing"
  4. Add new 'Text' block to Matrix field
  5. 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?

  • Unsure
  • Fresh install of Calendar
  • After upgrading from older Calendar version
  • After upgrading from older Craft version
  • After a change to hosting configuration

Previous Calendar Version

No response

Metadata

Metadata

Labels

6.0Calendar 6.0 releaseconfirmedBug has been confirmedfixingCurrently working in a fix for the issuelimitationA current limitation of the plugin

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions