I am trying to add a new column to a table and I added the column using hook_update_N().

function cca_update_8902(&$sandbox) {
  db_add_field('cca_findings', 'cca_topic_id', array(
    'type' => 'float',
      'not null' => FALSE,
      'description' => 'interim fields addition',

My field is also defined in the cca_findings class as property.

class cca_findings extends ContentEntityBase {

  use EntityChangedTrait;
  * {@inheritdoc}
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields['cca_topic_name'] = BaseFieldDefinition::create('string_long')
      ->setSettings(['max_length' => 2047]);
    $fields['meanvalue'] = BaseFieldDefinition::create('float')

    $fields['cca_topic_id'] = BaseFieldDefinition::create('float')

    return $fields;


So, the column has been created correctly, but the values are not going on the backend. I feel I am missing something.

What is missing in the code I wrote?

  • @kiamlaluno thanks for cleaning up the question. Can you help on this? I am struck with this one :(
    – Rick
    Nov 30, 2020 at 20:28
  • You don't need to create the database fields; that is done from Drupal core. I am looking for a Drupal core module that updates its own entity definition, but it's not simple to find one.
    – apaderno
    Nov 30, 2020 at 20:34
  • Ahh the table belongs to a custom module. So I don't want to use D8 module. I want to create through my custom module
    – Rick
    Nov 30, 2020 at 20:35

1 Answer 1


As described in Support for automatic entity updates has been removed, when a module needs to update its own entity, and the update must change the database schema, the module needs to implement hook_post_update_NAME(). That is what the Taxonomy module does in taxonomy_post_update_make_taxonomy_term_revisionable() to make its entity revisionable.

The key code lines in that hook, which show how a new entity field is added, are the following ones.

$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$entity_type = $definition_update_manager->getEntityType('taxonomy_term');
$field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions('taxonomy_term');
$field_storage_definitions['revision_id'] = BaseFieldDefinition::create('integer')
  ->setLabel(new TranslatableMarkup('Revision ID'))
  ->setSetting('unsigned', TRUE);
$field_storage_definitions['revision_default'] = BaseFieldDefinition::create('boolean')
  ->setLabel(new TranslatableMarkup('Default revision'))
  ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.'))
$definition_update_manager->updateFieldableEntityType($entity_type, $field_storage_definitions, $sandbox);

In your case, the code for hook_post_update_NAME() would be similar to the following one.

$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$entity_type = $definition_update_manager->getEntityType('entity_machine_name');
$field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions('entity_machine_name');
$field_storage_definitions['meanvalue'] = BaseFieldDefinition::create('float')
$field_storage_definitions['cca_topic_id'] = BaseFieldDefinition::create('float')
$definition_update_manager->updateFieldableEntityType($entity_type, $field_storage_definitions, $sandbox);

Replace 'entity_machine_name' with the machine name of the entity.

  • @kiamulaluno where should code go? in the .install file?
    – Rick
    Nov 30, 2020 at 21:50
  • 1
    hook_post_update_NAME() implementations go in <module_name>.post_update.php. Replace <module_name> with the module machine name.
    – apaderno
    Nov 30, 2020 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.