Non-existent node author user IDs are silently converted to 0 (?) (without validation errors)

Created on 18 November 2014, almost 10 years ago
Updated 28 June 2024, 2 months ago

Problem/Motivation

If the node author is set to a non-existing user, either on create or update, upon save this data gets lost.

This was discovered running migrations: when nodes are created first and later the users then the association gets lost.

Proposed resolution

Allow non-existent user ids to be saved?

Remaining tasks

Discuss

User interface changes

n/a

API changes

Non-existent user ids will now be saved rather than replaced with the current users id.

🐛 Bug report
Status

Postponed: needs info

Version

11.0 🔥

Component
Entity 

Last updated about 14 hours ago

Created by

🇦🇺Australia benjy

Live updates comments and jobs are added and updated live.
  • Triaged core major

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

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.

  • 🇬🇧United Kingdom nlisgo

    I can recreate the issue in 11.x.

    drush ev "\Drupal::entityTypeManager()->getStorage('node')->create(['title' => 'test', 'type' => 'article', 'uid' => 12345])->save();"

    SELECT * FROM node_field_data;
    +-----+-----+---------+----------+--------+-----+-------+------------+------------+---------+--------+------------------+-------------------------------+
    | nid | vid | type    | langcode | status | uid | title | created    | changed    | promote | sticky | default_langcode | revision_translation_affected |
    +-----+-----+---------+----------+--------+-----+-------+------------+------------+---------+--------+------------------+-------------------------------+
    |   2 |   2 | article | en       |      1 |   0 | test  | 1719579473 | 1719579473 |       1 |      0 |                1 |                             1 |
    +-----+-----+---------+----------+--------+-----+-------+------------+------------+---------+--------+------------------+-------------------------------+
    
  • Status changed to Postponed: needs info 2 months ago
  • 🇬🇧United Kingdom nlisgo

    Since we have focused in on the handling of setting a uid which doesn't correspond to a user on Node, I thought I would see how the same scenario is handled for comments.

    drush ev "\Drupal::entityTypeManager()->getStorage('comment')->create(['entity_id' => 2, 'entity_type' => 'node', 'field_name' => 'comment', 'comment_type' => 'comment', 'uid' => 12345, 'comment_body' => ['value' => 'This is the comment body', 'format' => 'basic_html']])->save();"

    The result of this is to store the uid is given:

    SELECT * FROM comment_field_data;
    +-----+--------------+----------+--------+-------+------+-----------+---------+------+------+----------+----------+------------+------------+--------+-------------+------------+------------------+
    | cid | comment_type | langcode | status | uid   | pid  | entity_id | subject | name | mail | homepage | hostname | created    | changed    | thread | entity_type | field_name | default_langcode |
    +-----+--------------+----------+--------+-------+------+-----------+---------+------+------+----------+----------+------------+------------+--------+-------------+------------+------------------+
    |   1 | comment      | en       |      0 | 12345 | NULL |         2 | NULL    | NULL | NULL | NULL     | NULL     | 1719581477 | 1719581477 | 01/    | node        | comment    |                1 |
    +-----+--------------+----------+--------+-------+------+-----------+---------+------+------+----------+----------+------------+------------+--------+-------------+------------+------------------+
    

    It seems there is a desire to not allow the storing of nodes if a specified uid does not match a stored user. See #9. But, if this is expensive perhaps it is better to just allow the storing of the uid as provided. This could be achieved by removing the following:

    From \Drupal\node\Entity\Node::presave()

    // If no owner has been set explicitly, make the anonymous user the owner.
          if (!$translation->getOwner()) {
            $translation->setOwnerId(0);
          }
    

    Is there any appetite to just remove this code and not throw an exception at all?

Production build 0.71.5 2024