Updated: Comment #0

@plach came up with this idea, so kudos go to him.

Problem/Motivation

The names of the entity tables for entity types are exposed in the entity type annotation even though, they really are an implementation detail of the storage.

Proposed resolution

Once #2183231: Make ContentEntityDatabaseStorage generate static database schemas for content entities goes in we are technically able to provide sane defaults for the table names, so that they no longer need to live in the annotation, but an entity type does not need to override ContentEntityDatabaseStorage either in order to provide the table names. It can still do that, though, if it does wants to use non-default names.

Remaining tasks

- Commit #2183231: Make ContentEntityDatabaseStorage generate static database schemas for content entities

User interface changes

None.

API changes

  • Entity type no longer define their table names in the annotation
  • EntityTypeInterface::getBaseTable(), EntityTypeInterface::getRevisionTable(), EntityTypeInterface::getDataTable() and EntityTypeInterface::getRevisionDataTable() are removed without replacement. This is a good thing, as they are an implementation detail.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

plach’s picture

Status: Postponed » Active
plach’s picture

Issue tags: +entity storage

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Berdir’s picture

Maybe as a start we should just have defaults instead of removing the ability to set them, which honestly seems very unlikely to get to any time soon, simply because this would break anything that would not follow the default/enforced table name pattern already.

Berdir’s picture

Status: Active » Needs review
FileSize
64.46 KB

That would look like this.

Just getting rid of those weird *property* table names would be worth doing this :p

Berdir’s picture

+++ b/core/lib/Drupal/Core/Entity/ContentEntityType.php
@@ -54,6 +54,21 @@ public function __construct($definition) {
+    // Set default table names.
+    if (empty($this->base_table)) {
+      $this->base_table = $this->id();

I guess this will break contact_message not having a table at all, lets see.

Status: Needs review » Needs work

The last submitted patch, 9: default-entity-tables-2232465-9.patch, failed testing. View results

Sam152’s picture

Do we still want to do this part of the issue summary:

EntityTypeInterface::getBaseTable(), EntityTypeInterface::getRevisionTable(), EntityTypeInterface::getDataTable() and EntityTypeInterface::getRevisionDataTable() are removed without replacement. This is a good thing, as they are an implementation detail.

I imagine we'll need to deprecate those instead of removing them?

Berdir’s picture

No, my suggestion would neither remove or deprecate those.

Even deprecating them at this time seems unrealistic IMHO, the current views integration is not going away any time soon and that works on tables and needs those methods.

I'll have a look at the test fails later, looks like at least one is because we have an entity type that is not using lowercase ID... :)

amateescu’s picture

Yep, we won't be able to remove them until D9, but this issue should still be about deprecating them.

Discussed this with @Berdir and agreed to move #9 to a separate issue, so we can keep this one with its initial scope/purpose.

plach’s picture

I agree we are not allowed to remove the keys from the entity type annotation until D9.

The only viable solution for D8 would be to deprecate them (and the related getters) but still make sure they have priority over the defaults provided when instantiating the storage (or soon the table mapping), so we keep BC but we basically make them optional. I recently defined a custom entity type and I realized atm they are not optional at all, there is core code that (silently) breaks if they are missing.

In fact, we can only deprecate them once the Views code relies on the table mapping, at very least to retrieve those values. We probably don't need for #2079019: Make Views use SqlEntityStorageInterface to happen, though.

plach’s picture

I recently defined a custom entity type and I realized atm they are not optional at all, there is core code that (silently) breaks if they are missing.

Which is what #15 was subtly trying to tell me, I guess :D

amateescu’s picture

@Berdir opened #2958963: Provide default values for the table name annotations for content entities for his patch from #9 and I also opened #2960037: Add a way to get the primary table of an entity type from TableMappingInterface to provide a replacement for the $entity_type->get(Base|Data|Revision|RevisionData)Table() methods that will be deprecated here.

amateescu’s picture

Title: Remove table names from entity annotations » Deprecate table names from entity definitions
Version: 8.5.x-dev » 8.6.x-dev
Status: Needs work » Needs review
Issue tags: +Workflow Initiative
FileSize
9.1 KB

Here's a start for this. It changes both the entity type definition and the default SQL storage to get the table names from the table mapping, while keeping the ability to specify custom table names in the entity definition.

Status: Needs review » Needs work

The last submitted patch, 19: 2232465-19.patch, failed testing. View results

amateescu’s picture

Status: Needs work » Needs review
FileSize
7.25 KB
1.85 KB
+++ b/core/lib/Drupal/Core/Entity/EntityType.php
@@ -710,7 +711,10 @@ public function getBundleLabel() {
-    return $this->base_table;
+    $storage = \Drupal::entityTypeManager()->getStorage($this->id);
+    if ($storage instanceof SqlEntityStorageInterface) {
+      return $storage->getTableMapping()->getBaseTable();
+    }

This is actually a BC break since code that was relying on NULL output from these methods on EntityType will now receive the auto-generated table name.

A lot of unit tests will need to be refactored here, so let's see first if we're ok with the general approach of this patch.

Status: Needs review » Needs work

The last submitted patch, 21: 2232465-21.patch, failed testing. View results

amateescu’s picture

Status: Needs review » Needs work

The last submitted patch, 24: 2232465-24-combined.patch, failed testing. View results

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Status: Needs review » Needs work

The last submitted patch, 27: 2232465-27.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.