Fix NOT NULL handling in the entity storage and 'primary key' changes when updating the storage definition of an identifier field

Created on 5 January 2017, almost 8 years ago
Updated 26 September 2024, about 2 months ago

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.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component

entity system

Created by

πŸ‡·πŸ‡΄Romania amateescu

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

  • Triaged core major

    There is consensus among core maintainers that this is a major issue. Only core committers should add this tag.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡¦Ukraine pingwin4eg Zaporizhia πŸ‡ΊπŸ‡¦

    Shouldn't the change record be in the draft status at this stage? I see it's published, but the issue isn't committed yet, so nothing described in it works.

Production build 0.71.5 2024