When removing a file from an entity then uploading one with exactly the same name the following error is produced when trying to save the entity:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'SomeHash' for key 'hash': INSERT INTO {redirect}...

Filefield Paths generates a hash for each redirect it creates and uses it to check if the redirect already exists. Currently Filefield Paths generates this hash using the target or destination path, while the Redirect module uses the source path with the leading slash removed.

This results in Filefield Paths never finding the existing redirect and trying to create a new one.

To resolve this, the module needs to generate a hash the same way as Redirect does. See Drupal\redirect\Entity\Redirect.

Comments

imclean created an issue. See original summary.

imclean’s picture

StatusFileSize
new623 bytes

Added a space.

imclean’s picture

Issue summary: View changes
imclean’s picture

Issue summary: View changes
brooke_heaton’s picture

Tested #2 - working well for me.

I came across this issue when running Behat tests to create File Media entities - I use the same source file in my tests. The patch resolves the duplicate hash issue.

w.drupal’s picture

Patch re-rolled to be consistent with the last dev version

w.drupal’s picture

Also the language was not taken into account

chrisck’s picture

Tested patch #7 and it works well on D9.1

chrisck’s picture

Status: Needs review » Reviewed & tested by the community

Setting to RTBC.

voleger’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Need to add tests for that case

junaidpv’s picture

#7 works.

imclean’s picture

+++ b/src/Redirect.php
@@ -79,9 +79,10 @@ class Redirect implements RedirectInterface {
+    $redirect->setLanguage($language->getId());    ¶

This looks like an unrelated change so should probably be a separate issue.

nicklasmf’s picture

I have an issue where this duplicated hash happens when trying to upload a file multiple times to different directories using Media Library.
Patch #7 works for me as well.