# 4.2: Can't send email, error 500, foreign key constraint fails

**URL:** <https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933>\
**Category:** Product Support\
**Tags:** mautic-4\
**Created:** [March 6, 2022, 3:17am UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933 "2022-03-06T03:17:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cspenn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/cspenn/32/1991_2.png) [@cspenn](https://forum.mautic.org/u/cspenn)\
**Post date:** [March 6, 2022, 3:17am UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/1 "2022-03-06T03:17:50Z")

</div>

**Your software**  
My Mautic version is: 4.2  
My PHP version is: 7.4.27  
My Database type and version is: MariaDB 10.5.15

**Your problem**  
My problem is: Error 500 on attempting to send an email.

These errors are showing in the log:

[2022-03-05 22:08:11] mautic.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException: "An exception occurred while executing ‘INSERT INTO mauticemail\_stats (email\_address, date\_sent, is\_read, is\_failed, viewed\_in\_browser, date\_read, tracking\_hash, retry\_count, source, source\_id, tokens, open\_count, last\_opened, open\_details, email\_id, lead\_id, list\_id, ip\_id, copy\_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)’ with params [{{email content removed}} “, 0, null, “a:0:{}”, 25, “77”, 1, null, “4bfeaf7a3350744da59befbff9c56667”]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mautic`.`mauticemail_stats`, CONSTRAINT `FK_8EA2B849A8752772` FOREIGN KEY (`copy_id`) REFERENCES `mauticemail_copies` (`id`) ON DELETE SET NULL) at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:68, Doctrine\DBAL\Driver\PDO\Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mautic`.`mauticemail_stats`, CONSTRAINT `FK_8EA2B849A8752772` FOREIGN KEY (`copy_id`) REFERENCES `mauticemail_copies` (`id`) ON DELETE SET NULL) at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mautic`.`mauticemail_stats`, CONSTRAINT `FK_8EA2B849A8752772` FOREIGN KEY (`copy_id`) REFERENCES `mauticemail_copies` (`id`) ON DELETE SET NULL) at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:112)”}

Steps I have tried to fix the problem: reinstall files, ran php bin/console doctrine:migration:status and php bin/console doctrine:migration:migrate with no errors and nothing outstanding.

I have also gone into the MariaDB table for mauticemail\_copies and deleted the most recent edition along with the corresponding row in mauticemails with no success.

---

<div class="post-metadata">

**Author:** ![cspenn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/cspenn/32/1991_2.png) [@cspenn](https://forum.mautic.org/u/cspenn)\
**Post date:** [March 13, 2022, 2:33am UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/2 "2022-03-13T02:33:13Z")

</div>

Still unable to fix the problem. Deleting the email has no effect. Have also run optimize table on the affected tables listed in the query.

---

<div class="post-metadata">

**Author:** ![cspenn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/cspenn/32/1991_2.png) [@cspenn](https://forum.mautic.org/u/cspenn)\
**Post date:** [March 20, 2022, 6:09pm UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/3 "2022-03-20T18:09:39Z")

</div>

Still unable to get past this problem. In my shell script, these are the commands I run:

find /home/mauticmail -type f -size 0b -delete &  
php /var/www/html/bin/console cache:clear  
php /var/www/html/bin/console mautic:social:monitoring &  
php /var/www/html/bin/console mautic:segments:update --force &  
php /var/www/html/bin/console mautic:segments:rebuild &  
php /var/www/html/bin/console mautic:messages:send &  
php /var/www/html/bin/console mautic:import &  
php /var/www/html/bin/console mautic:emails:send --force &  
php /var/www/html/bin/console mautic:email:fetch &  
php /var/www/html/bin/console mautic:campaigns:trigger --force &  
php /var/www/html/bin/console mautic:campaigns:rebuild --force &  
php /var/www/html/bin/console mautic:campaigns:messages &  
php /var/www/html/bin/console mautic:campaigns:messagequeue &  
php /var/www/html/bin/console mautic:broadcasts:send &  
php /var/www/html/bin/console mautic:maintenance:cleanup --days-old=365 --no-interaction &  
php /var/www/html/bin/console mautic:queue:process &

---

<div class="post-metadata">

**Author:** ![renatocron](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/renatocron/32/6936_2.png) [@renatocron](https://forum.mautic.org/u/renatocron)\
**Post date:** [March 20, 2022, 11:07pm UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/4 "2022-03-20T23:07:46Z")

</div>

Can you post “SHOW CREATE TABLE” for mauticemail\_stats, mauticemail\_copies

maybe the types are not set correctly (eg: int(11) vs int(10) on the other table)

---

<div class="post-metadata">

**Author:** ![escopecz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/escopecz/32/370_2.png) [@escopecz](https://forum.mautic.org/u/escopecz)\
**Post date:** [March 21, 2022, 8:39am UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/5 "2022-03-21T08:39:37Z")

</div>

Run this command and copy-paste its output please:

```auto
bin/console doctrine:schema:update --dump-sql | grep email

```

---

<div class="post-metadata">

**Author:** ![cspenn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/cspenn/32/1991_2.png) [@cspenn](https://forum.mautic.org/u/cspenn)\
**Post date:** [March 21, 2022, 11:12am UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/6 "2022-03-21T11:12:47Z")

</div>

CREATE INDEX mauticunique\_identifier\_search ON mauticcompanies (companyemail, companyname);

---

<div class="post-metadata">

**Author:** ![cspenn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/cspenn/32/1991_2.png) [@cspenn](https://forum.mautic.org/u/cspenn)\
**Post date:** [March 21, 2022, 11:13am UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/7 "2022-03-21T11:13:33Z")

</div>

| mauticemail\_stats | CREATE TABLE `mauticemail_stats` (  
`id` bigint(20) unsigned NOT NULL AUTO\_INCREMENT,  
`email_id` int(10) unsigned DEFAULT NULL,  
`lead_id` bigint(20) unsigned DEFAULT NULL,  
`list_id` int(10) unsigned DEFAULT NULL,  
`ip_id` int(10) unsigned DEFAULT NULL,  
`copy_id` varchar(32) DEFAULT NULL,  
`email_address` varchar(191) NOT NULL,  
`date_sent` datetime NOT NULL,  
`is_read` tinyint(1) NOT NULL,  
`is_failed` tinyint(1) NOT NULL,  
`viewed_in_browser` tinyint(1) NOT NULL,  
`date_read` datetime DEFAULT NULL,  
`tracking_hash` varchar(191) DEFAULT NULL,  
`retry_count` int(11) DEFAULT NULL,  
`source` varchar(191) DEFAULT NULL,  
`source_id` int(11) DEFAULT NULL,  
`tokens` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4\_unicode\_ci DEFAULT NULL COMMENT ‘(DC2Type:array)’,  
`open_count` int(11) DEFAULT NULL,  
`last_opened` datetime DEFAULT NULL,  
`open_details` longtext DEFAULT NULL COMMENT ‘(DC2Type:array)’,  
`generated_sent_date` date GENERATED ALWAYS AS (concat(year(`date_sent`),’-’,lpad(month(`date_sent`),2,‘0’),’-’,lpad(dayofmonth(`date_sent`),2,‘0’))) VIRTUAL COMMENT ‘(DC2Type:generated)’,  
PRIMARY KEY (`id`),  
KEY `IDX_8EA2B849A832C1C9` (`email_id`),  
KEY `IDX_8EA2B84955458D` (`lead_id`),  
KEY `IDX_8EA2B8493DAE168B` (`list_id`),  
KEY `IDX_8EA2B849A03F5E9F` (`ip_id`),  
KEY `IDX_8EA2B849A8752772` (`copy_id`),  
KEY `mauticstat_email_search` (`email_id`,`lead_id`),  
KEY `mauticstat_email_search2` (`lead_id`,`email_id`),  
KEY `mauticstat_email_failed_search` (`is_failed`),  
KEY `mauticis_read_date_sent` (`is_read`,`date_sent`),  
KEY `mauticstat_email_hash_search` (`tracking_hash`),  
KEY `mauticstat_email_source_search` (`source`,`source_id`),  
KEY `mauticemail_date_sent` (`date_sent`),  
KEY `mauticemail_date_read_lead` (`date_read`,`lead_id`),  
KEY `mauticgenerated_sent_date_email_id` (`generated_sent_date`,`email_id`),  
CONSTRAINT `FK_8EA2B8493DAE168B` FOREIGN KEY (`list_id`) REFERENCES `mauticlead_lists` (`id`) ON DELETE SET NULL,  
CONSTRAINT `FK_8EA2B84955458D` FOREIGN KEY (`lead_id`) REFERENCES `mauticleads` (`id`) ON DELETE SET NULL,  
CONSTRAINT `FK_8EA2B849A03F5E9F` FOREIGN KEY (`ip_id`) REFERENCES `mauticip_addresses` (`id`),  
CONSTRAINT `FK_8EA2B849A832C1C9` FOREIGN KEY (`email_id`) REFERENCES `mauticemails` (`id`) ON DELETE SET NULL,  
CONSTRAINT `FK_8EA2B849A8752772` FOREIGN KEY (`copy_id`) REFERENCES `mauticemail_copies` (`id`) ON DELETE SET NULL  
) ENGINE=InnoDB AUTO\_INCREMENT=6454505 DEFAULT CHARSET=utf8mb4 ROW\_FORMAT=DYNAMIC |

---

<div class="post-metadata">

**Author:** ![cspenn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/cspenn/32/1991_2.png) [@cspenn](https://forum.mautic.org/u/cspenn)\
**Post date:** [March 21, 2022, 11:14am UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/8 "2022-03-21T11:14:04Z")

</div>

> [@renatocron](#):
>
> mauticemail\_copies

| mauticemail\_copies | CREATE TABLE `mauticemail_copies` (  
`id` varchar(32) COLLATE utf8mb4\_unicode\_ci NOT NULL,  
`date_created` datetime NOT NULL,  
`body` longtext COLLATE utf8mb4\_unicode\_ci DEFAULT NULL,  
`subject` longtext COLLATE utf8mb4\_unicode\_ci DEFAULT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4\_unicode\_ci ROW\_FORMAT=DYNAMIC |

---

<div class="post-metadata">

**Author:** ![renatocron](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/renatocron/32/6936_2.png) [@renatocron](https://forum.mautic.org/u/renatocron)\
**Post date:** [March 29, 2022, 3:32pm UTC](https://forum.mautic.org/t/4-2-cant-send-email-error-500-foreign-key-constraint-fails/22933/9 "2022-03-29T15:32:12Z")

</div>

the table column on mauticemail\_stats is  
`copy_id` varchar(32) DEFAULT NULL,

and on mauticemail\_copies it’s defined as  
`id` varchar(32) COLLATE utf8mb4\_unicode\_ci NOT NULL,

so you need to make sure both tables are encoded with the same collation.

You need to convert all the tables into the same (preferability utf8mb4\_unicode\_ci) on all tables, there’s some other posts on this forum and github related to this issue. One issue you may face after trying to convert the columns to utf8mb4\_unicode\_ci, is that the index is bigger than what’s supported on mysql/mariadb, so you need to reduce the column from 255 to 191 chars

> <https://dba.stackexchange.com/questions/141149/wordpress-using-varchar255-for-index-with-innodb-and-utf8mb4-unicode-ci>
