Problem/Motivation

For translatable + revisionable content entities, core builds SQL joins that include a language condition when querying revision data.
In this path, the language column can be hard-coded as `langcode` for non-data-table joins, including the revision data table join.

This breaks entity types that define a custom langcode key (for example `language`) instead of the default `langcode`.
Result: SQL errors like “Unknown column ...field_revision.langcode in on clause” when opening translation/revision-related pages.

This is a correctness bug in SQL table alias/column resolution for entity query joins.

Steps to reproduce

1. Create a custom content entity type that is:
- translatable
- revisionable
- uses a custom langcode key (not named `langcode`)
2. Enable Content Translation for that entity type.
3. Create an entity and at least one revision.
4. Open the Translate page for that entity (or trigger a query that resolves latest translation-affected revision).
5. Observe SQL exception: unknown column `.langcode` in JOIN condition.

Proposed resolution

When selecting the language key for table joins, treat both data and revision data tables as entity-language-key tables.

Use the entity’s configured langcode key for:
- $entity_type->getDataTable()
- $entity_type->getRevisionDataTable()

Keep fallback behavior for other tables.

Example approach:

$data_tables = [$entity_type->getDataTable(), $entity_type->getRevisionDataTable()];
$langcode_key = in_array($table, $data_tables) ? $entity_type->getKey('langcode') : 'langcode';

This ensures joins target the actual language column defined by the entity type.

Remaining tasks

None.

User interface changes

None.

Introduced terminology

None.

API changes

No public API changes.
Internal query table/column resolution logic is corrected.

Data model changes

None.
No schema changes; only SQL generation logic is fixed.

Release notes snippet

Fixed an EntityQuery bug where revision-data table joins could hard-code `langcode`, causing SQL errors for translatable revisionable entities that use a custom langcode key. Translation/revision queries now correctly use the entity’s configured language key.

Issue fork drupal-3585716

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

lisotton created an issue. See original summary.

lisotton’s picture

Issue summary: View changes

lisotton’s picture

Assigned: lisotton » Unassigned
Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thank you for reporting. Can we get a test case showing the problem as a next step please

Thanks

lisotton’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
smustgrave’s picture

Status: Needs review » Needs work

Since the tests run in isolation the try/finally shouldn't be needed.

lisotton’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative

Ran a new pipeline to rerun tests for random failure (it was random)

Feedback appears to be addressed

godotislate’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for the work on this. It's close, just had one minor nit. OK to self-RTBC once change is made.

I did think there must have been a more direct way to test that the query produces the expected result, but then I saw there are no revisionable entity types in core that use a custom langcode key. The only one I found that has a custom langcode key is entity_test_mul_langcode_key, but that one's not revisionable. So short of creating a test entity type just for this purpose, I think the test is fine as is.
Test only job fails as expected: https://git.drupalcode.org/issue/drupal-3585716/-/jobs/9586809

lisotton’s picture

Status: Needs work » Reviewed & tested by the community

@godotislate I applied your suggestion in the MR, so I'm moving this back to RTBC.

  • godotislate committed 45045440 on main
    fix: #3585716 EntityQuery uses hard-coded langcode in revision-data...

  • godotislate committed 1b8823ac on 11.x
    fix: #3585716 EntityQuery uses hard-coded langcode in revision-data...

  • godotislate committed 2da39e65 on 11.4.x
    fix: #3585716 EntityQuery uses hard-coded langcode in revision-data...
godotislate’s picture

Version: main » 11.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 4504544 to main, 1b8823a to 11.x, and 2da39e6 to 11.4.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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