I have a configuration reference, so there is a taxonomy with a field "content type" which references to any number of content types.

Only if I enter > 1 content type as reference I get an error message

Drupal\Core\Entity\EntityStorageException SQLSTATE[23000]: entity_usage Integrity constraint violation: 1062 Duplicate entry for key 'PRIMARY': INSERT INTO {entity_usage}

'0-node_type-352-taxonomy_term-de-0-entity_reference-field_refere' for key 'PRIMARY': INSERT INTO {entity_usage} (target_id_string, target_type, source_id, source_type, source_langcode, source_vid, method, field_name, count) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8); Array
(
    [:db_insert_placeholder_0] => event
    [:db_insert_placeholder_1] => node_type
    [:db_insert_placeholder_2] => 352
    [:db_insert_placeholder_3] => taxonomy_term
    [:db_insert_placeholder_4] => de
    [:db_insert_placeholder_5] => 0
    [:db_insert_placeholder_6] => entity_reference
    [:db_insert_placeholder_7] => field_reference_content_type
    [:db_insert_placeholder_8] => 1
)
 in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()</em> (line 829</em> of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Probably Entity Usage must exclude saving data on Entity References to configurations to avoid this since they donnot have an unique target id.

Comments

demonde created an issue. See original summary.

marcoscano’s picture

StatusFileSize
new21.26 KB

Can you reproduce this in a clean install? If so, could you please share the steps to reproduce?

I've just tested it in a vanilla D8 that I have around, adding to a taxonomy term an entity_reference field pointing to a content type, and I did not see any error, and the usages seem to end up correctly in the DB:

So I wonder if I'm missing some important step to reproduce the bug.

Thanks!

marcoscano’s picture

Priority: Major » Normal

Also, moving to normal because you should probably be able to mitigate this by tweaking what is tracked and what not, in the configuration page.

demonde’s picture

Maybe I was not clear enough in the bug report.

The entity reference to "content type" is a multi value field and the error appears if I have two field values.

In your attached example above you have two different source id's but in my one the source id is identical.

marcoscano’s picture

Version: 8.x-2.0-alpha4 » 8.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new969 bytes

Oh I see what you mean now. Indeed, once the string id column in the DB is not a primary key, the SQL insert thinks it's a duplicate record.

Can you test if the attached patch solves the problem for you?

(Edit: Don't forget to go to /update.php and run the DB updates after applying the patch)

demonde’s picture

I applied the patch and updated and it works. I can now reference >1 field value per field at a configuration reference.

But on the update routine via drush I get an error message "Invalid use of NULL value":

Performing entity_usage_update_8203                                  [ok]
Failed: SQLSTATE[22004]: Null value not allowed: 1138 Invalid use of [error]
NULL value: ALTER TABLE {entity_usage} ADD PRIMARY KEY (`target_id`,
`target_id_string`, `target_type`, `source_id`, `source_type`,
`source_langcode`, `source_vid`, `method`, `field_name`); Array
(
marcoscano’s picture

StatusFileSize
new1.63 KB
new1.52 KB

Ops, true, the new primary key cannot be NULL so we need to change that column too.

This should take care of that, let me know otherwise.

Thanks for the feeback!

demonde’s picture

I still got an update error so I ended up and simple reinstalled the module.

  • marcoscano committed 0f0655b on 8.x-2.x
    Issue #2980089 by marcoscano, demonde: Not working with configuration...
marcoscano’s picture

Status: Needs review » Fixed

Committed this for now, once it works for me without the error. Let's open a follow-up if anyone still experiences issues with this.

Thanks!

demonde’s picture

Great, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.