Problem/Motivation
Currently, only a few entity keys are automatically marked as NOT NULL, which is a problem because it gives the impression that entity keys are somehow special and leads to bugs like #2824851: EntityResource::patch() makes an incorrect assumption about entity keys, hence results in incorrect behavior.
Also, developers have to know about this limitation of entity API and override \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getSharedTableFieldSchema()
in order to mark fields as NOT NULL.
An additional (major) problem found is that when we are updating the storage definition of an identifier field (e.g. 'id' or 'revision_id'), the primary keys are not updated properly in the SQL schema.
This patch also uncovered a another bug:
- The entity storage schema stores incorrect field schema data for the identifier fields: both the 'id' and 'revision_id' fields are of type int
in their base tables, when they should be serial
instead
Proposed resolution
Since we can now mark field storages as required (#2390495: Support marking field storage definitions as required) we can also add the NOT NULL database constraint automatically. This will also improve the performance of indexes that are using those required fields.
Pass the new 'primary key' definition when re-adding the identifier field in \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::createSharedTableSchema()
.
Fix the field storage schema data for the identifier fields for all entity types.
Remaining tasks
Review.
User interface changes
Nope.
API changes
The 'not_null' parameter of SqlContentEntityStorageSchema::addSharedTableFieldIndex() will become deprecated.
Instead, you should set \Drupal\Core\Field\BaseFieldDefinition::setStorageRequired() on the respective field.
Data model changes
Nope.