Cannot reference an entity with a big int (size setting of ID field not propagated to reference field)

Created on 3 March 2016, almost 9 years ago
Updated 4 March 2024, 10 months ago

Problem/Motivation

I created a custom entity who's id field definition looks like this:

$fields['id'] = BaseFieldDefinition::create('integer')
  ->setLabel(t('ID'))
  ->setDescription(t('The ID of the thePlatform Media entity.'))
  ->setSettings([
    'size' => 'big',
  ])
  ->setReadOnly(TRUE);

Whenever I create an entity reference field on another entity and reference it to this entity, I get this error:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'field_video_target_id' at row 1: INSERT INTO {node__field_video} (entity_id, revision_id, bundle, delta, langcode, field_video_target_id) 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); Array ( [:db_insert_placeholder_0] => 752238 [:db_insert_placeholder_1] => 1204418 [:db_insert_placeholder_2] => video [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => en [:db_insert_placeholder_5] => 39742019557 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 757 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Drupal\Core\Database\Statement->execute(Array, Array) (Line: 615)
Drupal\Core\Database\Connection->query('INSERT INTO {node__field_video} (entity_id, revision_id, bundle, delta, langcode, field_video_target_id) 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)', Array, Array) (Line: 86)
Drupal\Core\Database\Driver\mysql\Connection->query('INSERT INTO {node__field_video} (entity_id, revision_id, bundle, delta, langcode, field_video_target_id) 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)', Array, Array) (Line: 37)
Drupal\Core\Database\Driver\mysql\Insert->execute() (Line: 1265)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->saveToDedicatedTables(Object, , Array) (Line: 853)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->doSaveFieldItems(Object) (Line: 268)
Drupal\Core\Entity\ContentEntityStorageBase->doSave(NULL, Object) (Line: 397)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 748)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 363)
Drupal\Core\Entity\Entity->save() (Line: 361)
Drupal\node\NodeForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 116)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 56)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 588)
Drupal\Core\Form\FormBuilder->processForm('node_video_form', Array, Object) (Line: 319)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 53)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object) (Line: 118)
Drupal\node\Controller\NodeController->add(Object)
call_user_func_array(Array, Array) (Line: 128)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 577)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 129)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 102)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 139)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 62)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 55)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 34)
Drupal\gc_api\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 637)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

It looks like the field schema always assumes the id is a standard size int rather than a big int. :(

Proposed resolution

Get the size of the field from the field definition and set it to the same.

Remaining tasks

User interface changes

None.

API changes

Allows entity reference to reference other entities that have a big int id.

Without that patch - this feature is simply not working, showing the fatal error. So we don't need any upgrade paths or migrations, we are only making this feature work as expected. This consensus was reached on Drupal Slack with @catch.

Data model changes

This patch simply fixes the entity reference table's target_id to be a big int.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated about 10 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States davidwbarratt

Live updates comments and jobs are added and updated live.
  • Triaged core major

    There is consensus among core maintainers that this is a major issue. Only core committers should add this tag.

  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    I added this to my test install that has commerce 2.38 enabled.
    Then I got an error:
    Could not load the "commerce_order_item" order item type. in Drupal\commerce_order\Entity\OrderItem::bundleFieldDefinitions() (line 509 of /var/www/drupal10.2/html/modules/contrib/commerce/modules/order/src/Entity/OrderItem.php)

    And the drush updatedb could not be run because of the error.

Production build 0.71.5 2024