Skip to content

Integrity constraint violation: 1062 Duplicate entry for key 'craft_content_elementId_siteId_unq_idx' #9130

@jsmrtn

Description

@jsmrtn

Description

I'm working on a plugin that, amongst other things, takes data from a CSV and lets the user select a site to upload the data into (Title, Slug, basic text fields, Matrix, etc.). All of the fields it's trying to put the data into are translatable and the entry exists for both sites (set to propagate into all sites that the section is enabled for).

If I set the site ID to the primary site then the saveElement runs successfully and all of the fields are correctly updated, but if I run it for the other site then it throws the error.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3636-4' for key 'craft_content_elementId_siteId_unq_idx'
The SQL being executed was: UPDATE `craft_content` SET `elementId`=3636, `siteId`=4, `title`='21', `field_customTitle`='1', `field_summary`='2', `field_seoMetaTitle`='18', `field_seoMetaDescription`='19', `field_seoKeywords`='20', `dateUpdated`='2021-07-06 15:45:12' WHERE `id`=1459
Error Info: Array
(
    [0] => 23000
    [1] => 1062
    [2] => Duplicate entry '3636-4' for key 'craft_content_elementId_siteId_unq_idx'
)
↵
Caused by: PDOException
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3636-4' for key 'craft_content_elementId_siteId_unq_idx'

It's a fairly simple function-$syncData is the field layout for the entry from the primary site, with the data from the CSV injected into it at the correct array keys to overwrite the current value with the value from the CSV

$element->siteId = 3; // Primary Site, works fine
// $element->siteId = 4; // Second site, errors out

if (isset($syncData['title'])) {
    $element->title = $syncData['title'];
    unset($syncData['title']);
}
if (isset($syncData['slug'])) {
     $element->slug = $syncData['slug'];
     unset($syncData['slug']);
}

$element->setFieldValues($syncData);
$success = Craft::$app->elements->saveElement($element, true, false);

I've tried to resave the entries, and also tried installing the fix FK plugin, both to no avail.

Additional info

  • Craft version: Craft Pro 3.6.17
  • PHP version: 7.4.2
  • Database driver & version: MySQL 5.7.26

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions