Is it possible to update / alter database schema?

Created on 21 November 2023, about 1 year ago
Updated 5 December 2023, 12 months ago

Is there a safe way to add fields (columns) to or to remove them from a datafield table containing data? The functionally related Custom field module provides a service for this, and I am wondering whether that would also be possible with the Datafield module as well. (Custom field does not support entity references currently, so I cannot use that module.)

💬 Support request
Status

Needs review

Version

1.0

Component

Documentation

Created by

🇮🇪Ireland marksmith

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @marksmith
  • Status changed to Needs review about 1 year ago
  • 🇫🇷France lazzyvn paris

    Adding new columns is supported.
    to delete the data field must be empty: You can backup your field_table_datafield then you can truncate field_table_datafield. Back in the field storage settings you can now delete your columns or change the hostname. then you can restore your field table _datafield

  • 🇮🇪Ireland marksmith

    Thanks for the quick reply! Is there a description how adding a new column to a datafield (already containing data) actually works? As far as I see, this cannot be achieved via the regular UI ("cannot be changed once data has been created").

  • 🇫🇷France lazzyvn paris

    Sorry I checked aggain You can't add new column because Drupal 10 field base is locked

    The SQL storage cannot change the schema for an existing field

    you can do by yourself with devel php

    $entityType= 'node';
    $fieldName= 'field_datas';
    $storage_definition = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entityType)[$fieldName];
    $saveToDefinition = \Drupal::service('entity.last_installed_schema.repository')->setLastInstalledFieldStorageDefinition($storage_definition);
    $schema = \Drupal::keyValue('entity.storage_schema.sql')->get("$entityType.field_schema_data.$fieldName");
    // Change your schemas
    ....
    // now save your fields schema
        \Drupal::keyValue('entity.storage_schema.sql')->set("$entityType.field_schema_data.$fieldName", $field_schema);
    
  • 🇮🇪Ireland marksmith

    I started to test this with the (for me) easier solution, the Field storage config export / import.

    Unfortunately this solution does not working for me, as it leads to the unable to alter the schema error: "Unexpected error during import with operation update for field.storage.node.field_name: The SQL storage cannot change the schema for an existing field (field_name in node entity) with data."

    I suppose that the scenario I am looking for - that one would want to alter the datafield schema already containing data at some future moment - is quite realistic, and it would be ideal to have a UI solution for this. I understand that this would require a good amount of work on this module though. So, thank you for the effort invested thus far.

  • 🇫🇷France lazzyvn paris

    After some research, Creating a ui to add/remove columns is not easy (most of the difficulty in the drupal core api). Sure I could do it if I had more time, but I'd wait for the client to pay for it. Actually I have 15 customers but they don't want to pay for this feature, for them they almost never change the structure after production. For them, recreating the field is faster than take time developing a feature for its intended use in just a few minutes :).

Production build 0.71.5 2024