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.