After generating an entity with Drupal console I noticed that several fields, including ones that I created manually, remain nullable in the database after using BaseFieldDefinition::setStorageRequired(TRUE) or BaseFieldDefinition::setRequired(TRUE)
even after uninstalling the entity's module and re-installing the module to recreate the table with fresh field definitions. Also note that these fields are not entity keys.
The snippets I'm using are as follows, and for instance, the "description" field remains nullable in MySQL:
/**
* Defines the CPAD Log entity.
*
* @ingroup cpad
*
* @ContentEntityType(
* id = "cpad_log",
* label = @Translation("CPAD Log"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "views_data" = "Drupal\cpad\Entity\CPADLogViewsData",
* "access" = "Drupal\cpad\CPADLogAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\cpad\CPADLogHtmlRouteProvider",
* },
* },
* base_table = "cpad_log",
* admin_permission = "administer cpad log entities",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* },
* links = {
* "canonical" = "/admin/structure/cpad_log/{cpad_log}",
* "collection" = "/admin/structure/cpad_log",
* },
* field_ui_base_route = "cpad.log.settings"
* )
*/
class CPADLog extends ContentEntityBase implements CPADLogInterface {
...
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
...
$fields['description'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Log Description'))
->setDescription(t('A description of what happend.'))
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE)
->setStorageRequired(TRUE)
->setRequired(TRUE);
...
}
...
}