Problem/Motivation

Entities that have a computed entity_reference field create a fatal error when using the EntityViewsData class for their views_data handler:

Drupal\Core\Entity\Sql\SqlContentEntityStorageException: Column information not available for the 'location' field. in Drupal\Core\Entity\Sql\DefaultTableMapping->getFieldColumnName() (line 439 of /opt/drupal/web/core/lib/Drupal/Core/Entity/Sql/DefaultTableMapping.php).

It seems that this error happens because EntityViewsData creates reverse relationships for all entity_reference fields and expects the field to be stored in the DB. But since this is a computed entity reference field, there is no DB storage.

Steps to reproduce

Add a computed entity reference field to an entity. Set the views_data handler to EntityViewsData. Clearing cache will display the error.

Proposed resolution

A simple workaround is to disable reverse relationships for computed entity reference fields (including a patch with this approach, but I'm not sure this should considered a final solution).

A complete solution needs to somehow trigger computation of the computed field, but since this likely involves complex business logic, I'm unsure if there is a universal solution. It might require a custom views handler for each field?

Remaining tasks

Test using a computed entity reference field.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Issue fork entity-3190436

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

paul121 created an issue. See original summary.

paul121’s picture

StatusFileSize
new765 bytes

This patch only provides a workaround for the issue. Reverse relationships will not be created for computed entity reference fields.

paul121’s picture

StatusFileSize
new2.13 KB

This patch creates a failing test case. Adds a computed entity reference field to the entity_test_enhanced entity type used for testing. I added the views module to a test case that uses this entity type since the views module is required to recreate the error.

paul121’s picture

Marking this for review. This is quite a simple change - my patch from #2 is sufficient for fixing the bug.

holo96’s picture

Status: Needs review » Reviewed & tested by the community

Just ran into same issue, there is no reason not to apply 3190436-2.patch even without test.

holo96’s picture

Also I wouldn't say it is workaround, but solution, there is no way to know how computed fields are computed. I just has to be done manually.

holo96’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new771 bytes

Actually same thing is happening with custom storage fields... So uploading new patch and setting this issue to needs review again...

I guess this will never be solved, but at least we tried :D

It is enough to call `hasCustomStorage` on field, as it also checks if field is computed

Test stays.

berdir’s picture

Status: Needs review » Needs work

The way this works is that you need to provide a combined patch of test and fix so that it can be verified that the test passes then, then it also has a chance of getting committed.

narendra.rajwar27’s picture

Status: Needs work » Needs review
StatusFileSize
new2.89 KB

Adding combined patch of test and fix.

alayham’s picture

Status: Needs review » Reviewed & tested by the community

Tested with 1.4 running on Drupal 9.5 dev, using a computed entity reference field attached to node bundle, referencing another node bundle.
Patch applied, error disappeared.

urvashi_vora made their first commit to this issue’s fork.

holo96’s picture

@Berdir Can we merge this one?

  • berdir committed a64d8864 on 8.x-1.x authored by urvashi_vora
    Issue #3190436 by paul121, urvashi_vora, Holo96: EntityViewsData Column...
berdir’s picture

Status: Reviewed & tested by the community » Fixed

Merged.

berdir’s picture

Status: Fixed » Closed (fixed)

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