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
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 3190436-9.patch | 2.89 KB | narendra.rajwar27 |
| #7 | 3190436-7.patch | 771 bytes | holo96 |
| #3 | 3190436-3.patch | 2.13 KB | paul121 |
Issue fork entity-3190436
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
Comment #2
paul121 commentedThis patch only provides a workaround for the issue. Reverse relationships will not be created for computed entity reference fields.
Comment #3
paul121 commentedThis patch creates a failing test case. Adds a computed entity reference field to the
entity_test_enhancedentity 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.Comment #4
paul121 commentedMarking this for review. This is quite a simple change - my patch from #2 is sufficient for fixing the bug.
Comment #5
holo96 commentedJust ran into same issue, there is no reason not to apply 3190436-2.patch even without test.
Comment #6
holo96 commentedAlso 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.
Comment #7
holo96 commentedActually 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.
Comment #8
berdirThe 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.
Comment #9
narendra.rajwar27Adding combined patch of test and fix.
Comment #10
alayham commentedTested 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.
Comment #13
holo96 commented@Berdir Can we merge this one?
Comment #15
berdirMerged.
Comment #16
berdir