Skip to main content
3 of 4
added 18 characters in body
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

There isn't any documentation about that.
In SqlContentEntityStorageSchema::getSharedTableFieldSchema(), there is the following comment. (The relevant part is For now, we only hardcode 'not null' to a few "entity keys", in order to keep their indexes optimized.)

// A shared table contains rows for entities where the field is empty
// (since other fields stored in the same table might not be empty), thus
// the only columns that can be 'not null' are those for required
// properties of required fields. For now, we only hardcode 'not null' to a
// few "entity keys", in order to keep their indexes optimized.
// @todo Fix this in https://www.drupal.org/node/2841291. 

Fix NOT NULL handling in the entity storage and 'primary key' changes when updating the storage definition of an identifier field has been opened to remove the code that mark as NOT NULL some of the entity keys given for an entity type.

As a side note, the current workaround isn't to manually edit the database schema, but to override the getSharedTableFieldSchema() method implemented by the entity storage_schema handler.
This is what done by the File entity type, for example.

handlers = {
  "storage" = "Drupal\file\FileStorage",
  "storage_schema" = "Drupal\file\FileStorageSchema",
  "access" = "Drupal\file\FileAccessControlHandler",
  "views_data" = "Drupal\file\FileViewsData",
},
base_table = "file_managed",
entity_keys = {
  "id" = "fid",
  "label" = "filename",
  "langcode" = "langcode",
  "uuid" = "uuid",
  "owner" = "uid",
}

Drupal\file\FileStorageSchema::getSharedTableFieldSchema() contains the following code.

// Entity keys automatically have not null assigned to TRUE, but for the
// file entity, NULL is a valid value for uid.
if ($field_name === 'uid') {
  $schema['fields']['uid']['not null'] = FALSE;
}
return $schema;
avpaderno
  • 97.9k
  • 15
  • 164
  • 284