Problem/Motivation

The file entity does not declare the uri base field to be required, but saving a file entity without a uri will get an integrity constraint violation (Column uri cannot be NULL) inserting the file_managed row. This is a problem for me in #2590993: Create stub entities with proper default values - that issue will populate required fields with appropriate values when creating migration stubs, but needs to know that a field is required to do that.

Proposed resolution

A simple setRequired(TRUE) on the base field.

Remaining tasks

Add a test.

User interface changes

None

API changes

None

Data model changes

None

Comments

mikeryan created an issue. See original summary.

dawehner’s picture

Are you sure we don't need an update path for it?

mikeryan’s picture

I'm not sure... What would be updated? The table schema already has uri defined as NOT NULL.

alexpott’s picture

Status: Needs review » Needs work

We need to clear the cached definitions - just need an empty hook_update_N in the file module for this.

mikeryan’s picture

StatusFileSize
new1.15 KB
mikeryan’s picture

Status: Needs work » Needs review
mikeryan’s picture

Note that #2590993: Create stub entities with proper default values is no longer dependent on this patch - the "work-around" there turned out to be necessary anyway.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/file/file.install
    @@ -120,3 +120,19 @@ function file_requirements($phase) {
    + * The simple presence of this update function clears cached field definitions.
    + */
    +function file_update_8001() {
    +  // Uri base field is now required.
    +}
    

    The description of this function is shown in the UI when running updates. So it should be written in a way that tells the user what it does.

  2. +++ b/core/modules/file/src/Entity/File.php
    @@ -244,6 +244,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
           ->setDescription(t('The URI to access the file (either local or remote).'))
    +      ->setRequired(TRUE)
           ->setSetting('max_length', 255)
    

    I agree that it makes sense that URI should be required.

    But why does the schema have a NOT NULL if the field is not required? Are we altering the schema somewhere manually or automatically (e.g. due to the existence of an index on that column?)

    Checking.... Yeah, FileStorageSchema adds that not null automatically due to the call to addSharedTableFieldIndex().

    That doesn't seem 100% right.. it should only do that if the field is indeed required? Or maybe throw an exception if there's a mismatch?

    Might be good to have some feedback from @plach.

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.

kenorb’s picture

The following error happened when submitted /webform/%/test

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uri' cannot be null: UPDATE {file_managed} SET fid=:db_update_placeholder_0, type=:db_update_placeholder_1, uuid=:db_update_placeholder_2, langcode=:db_update_placeholder_3, uid=:db_update_placeholder_4, filename=:db_update_placeholder_5, uri=:db_update_placeholder_6, filemime=:db_update_placeholder_7, filesize=:db_update_placeholder_8, status=:db_update_placeholder_9, created=:db_update_placeholder_10, changed=:db_update_placeholder_11 WHERE fid = :db_condition_placeholder_0; Array ( [:db_update_placeholder_0] => 852 [:db_update_placeholder_1] => undefined [:db_update_placeholder_2] => f3f5989c-82ac-4e9b-8110-6b865e760501 [:db_update_placeholder_3] => en [:db_update_placeholder_4] => 1 [:db_update_placeholder_5] => [:db_update_placeholder_6] => [:db_update_placeholder_7] => application/octet-stream [:db_update_placeholder_8] => [:db_update_placeholder_9] => 0 [:db_update_placeholder_10] => 1499292302 [:db_update_placeholder_11] => 1499292302 [:db_condition_placeholder_0] => 852 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 777 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Drupal\Core\Database\Statement->execute(Array, Array) (Line: 624)
Drupal\Core\Database\Connection->query('UPDATE {file_managed} SET fid=:db_update_placeholder_0, type=:db_update_placeholder_1, uuid=:db_update_placeholder_2, langcode=:db_update_placeholder_3, uid=:db_update_placeholder_4, filename=:db_update_placeholder_5, uri=:db_update_placeholder_6, filemime=:db_update_placeholder_7, filesize=:db_update_placeholder_8, status=:db_update_placeholder_9, created=:db_update_placeholder_10, changed=:db_update_placeholder_11
WHERE fid = :db_condition_placeholder_0', Array, Array) (Line: 87)
Drupal\Core\Database\Driver\mysql\Connection->query('UPDATE {file_managed} SET fid=:db_update_placeholder_0, type=:db_update_placeholder_1, uuid=:db_update_placeholder_2, langcode=:db_update_placeholder_3, uid=:db_update_placeholder_4, filename=:db_update_placeholder_5, uri=:db_update_placeholder_6, filemime=:db_update_placeholder_7, filesize=:db_update_placeholder_8, status=:db_update_placeholder_9, created=:db_update_placeholder_10, changed=:db_update_placeholder_11
WHERE fid = :db_condition_placeholder_0', Array, Array) (Line: 148)
Drupal\Core\Database\Query\Update->execute() (Line: 822)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->doSaveFieldItems(Object) (Line: 263)
Drupal\Core\Entity\ContentEntityStorageBase->doSave('852', Object) (Line: 392)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 768)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 364)
Drupal\Core\Entity\Entity->save() (Line: 32)
Drupal\file\FileUsage\FileUsageBase->delete(Object, 'webform', 'webform_submission', '578', 0) (Line: 92)
Drupal\file\FileUsage\DatabaseFileUsageBackend->delete(Object, 'webform', 'webform_submission', '578', 0) (Line: 354)
Drupal\webform\Plugin\WebformElement\WebformManagedFileBase->postSave(Array, Object, ) (Line: 149)
Drupal\webform\Plugin\WebformElementManager->invokeMethod('postSave', Array, Object, , NULL) (Line: 1802)
Drupal\webform\Entity\Webform->invokeElements('postSave', Object, , NULL) (Line: 826)
Drupal\webform\WebformSubmissionStorage->invokeWebformElements('postSave', Object, ) (Line: 767)
Drupal\webform\WebformSubmissionStorage->doPostSave(Object, ) (Line: 395)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 768)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 364)
Drupal\Core\Entity\Entity->save() (Line: 668)
Drupal\webform\Entity\WebformSubmission->save() (Line: 1046)
Drupal\webform\WebformSubmissionForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('webform_submission_repairs_taps_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('webform_submission_repairs_taps_form', Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'default') (Line: 877)

I haven't test the patch yet.

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.

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

benedicte_w’s picture

StatusFileSize
new562 bytes

Here's the patch for drupal 8.7.4.

I choosed not to include changes on file.install and clear the caches manually.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

shivam kaushal’s picture

Assigned: Unassigned » shivam kaushal
shivam kaushal’s picture

Status: Needs work » Needs review
StatusFileSize
new579 bytes

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
larowlan’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll, +Bug Smash Initiative

This needs to reroll from #5 and include the changes from @Berdirs review

immaculatexavier’s picture

Status: Needs work » Needs review
StatusFileSize
new1.22 KB

Rerolled patch against #5 and included the changes from @Berdirs review

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

medha kumari’s picture

Version: 9.4.x-dev » 9.5.x-dev
Issue tags: -Needs reroll

Patch #24 applied successfully in 9.5.x-dev branch

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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.

@immaculatexavier thank you for the patch but please include an interdiff so we can see the changes.

From what I can tell #8.1 or #8.2 have not been addressed.
8.1 = the description appears to be the same from what it was
8.2 = dont' see any follow up for that.

Also the hook update is targeting D8 so that's not correct.

Thanks

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.