Problem/Motivation

Using Drupal 8.9 uploading files fails with an error
Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'type' cannot be null: INSERT INTO {download_count} (fid, uid, type, id, ip_address, referrer, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6);

Apparently in hook_entity_access, which is used in download_count_file_access (line 29 of download_count.module), the $operation == 'download' is also invoked during file upload. I do not find any information about this in the api documentation, except for one (Drupal 7) similar issue https://www.drupal.org/project/drupal/issues/2308347.

During upload the information of the referring entity is not known yet so the entity_type value is empty and we get the error.

Proposed resolution

Check if $references is empty before continuing to prevent the error: download_count.module
line 64: if (!empty($references)) {
line 105: }

CommentFileSizeAuthor
#3 3191787-3.patch3.1 KBmohit.bansal623

Comments

hansrossel created an issue. See original summary.

hansrossel’s picture

I suppose that the fact that the download operation is called during upload is rather a Drupal core issue and it is probably caused by the function valueCallback (line 60 of /core/modules/file/src/Element/ManagedFile.php) which is calling access('download')

if (!$file->access('download')) {
  $force_default = TRUE;
  break;
}

and /core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php which is calling valueCallback in line 324 during upload

// We depend on the managed file element to handle uploads.
$return = ManagedFile::valueCallback($element, $input, $form_state);
mohit.bansal623’s picture

Status: Active » Needs review
StatusFileSize
new3.1 KB
tlwatson’s picture

#3 works. I think it could be written more simply to continue/return if $references is empty, rather than wrapping everything in an 'if' statement.

usingsession’s picture

Version: 8.x-1.0-alpha1 » 2.0.0
Status: Needs review » Fixed

@mohit.bansal623 thanks for patch.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.