Cannot Reference Entity because of ValidReferenceConstraintValidator

Created on 3 April 2024, 9 months ago

Problem/Motivation

I cannot reference a entity when creating another entity which will reference the fresh created first entity.

I do Drupal for 15+ years and never had that problem. :-/ I call an endpoint in Drupal to create a bunch of entities inside a transaction, were a lot of entities will get a reference to the first entity I create. I create all entities as a normal user, BUT with uid of an user with Administrator permissions.

The creation of the first entity works without problems. I get an ID back and also ->validate() works without problems. When I create the next entity, which has a reference field to the first entity. The ValidReferenceConstraintValidator will always come to the conclusion, that the ID of the first object cannot be referenced.

I tracked down the problem into this method: $valid_target_ids = $handler->validateReferenceableEntities($target_ids); which will create a fairly simple query to find entities in the db, to check if the to reference entity truely exists. The query is something like this:

SELECT base_table.vid AS vid, base_table.nid AS nid
FROM node base_table
INNER JOIN node_field_data node_field_data ON node_field_data.nid = base_table.nid
WHERE (node_field_data.type IN ('case')) AND (node_field_data.nid IN ('8869'))

Bit every time, the internal Drupal Processing, probably the accessChecks, will come to the conclusion, that I cannot reference it.

I did removed the Transaction to see if the entity will be written to the node table, and yes it did and also the Query will return the ID inside an SQL Manager program.

Which brings me to the conclusion, that there is some right, which is currently unknown to me, which prevents the referencing of entities.

I do not habe permissions set on any reference field. There is also no permission known to me which allows referencing of entities. The strange thing is, that when I omit the reference, save the entity and after that set the reference and save again it works without problems. But because I do require the reference of to the first entity, I cannot use that workflow.

I tried to step through the sqlQuery build and execution, but the internals of that code is to advanced and to abstract for me to easily understand.

Has anybody a further idea, what could prevent me from referencing entities together in a transaction?

💬 Support request
Status

Closed: works as designed

Version

10.3

Component
Entity 

Last updated about 22 hours ago

Created by

🇩🇪Germany ro-no-lo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ro-no-lo
  • Status changed to Closed: works as designed 9 months ago
  • 🇩🇪Germany ro-no-lo

    Okay, that's life. I debugged this issue for like 4 hours, but find the unsatisfying solution after posting it on drupal.org within 10 minutes.

    For anybody run into the same thing: I do have the https://www.drupal.org/project/node_view_permissions module installed, which I had setup that the current user could not VIEW the first type of entity (see above text). This seem to lead to the awkward problem, that the user can create an entity, but cannot reference it later, because he could not "see" this type of reference. Therefore if he can't see it he propably shall not reference it.

    Don't like it, that I have to deal on a coding level with that. But that's what I found.

Production build 0.71.5 2024