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.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | incorrect_redirect_hash_generated-3045063-7.patch | 836 bytes | w.drupal |
| #6 | incorrect_redirect_hash_generated-3045063-6.patch | 696 bytes | w.drupal |
| #2 | incorrect_redirect_hash_generated-3045063-2.patch | 623 bytes | imclean |
Issue fork filefield_paths-3045063
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
imclean commentedAdded a space.
Comment #3
imclean commentedComment #4
imclean commentedComment #5
brooke_heaton commentedTested #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.
Comment #6
w.drupal commentedPatch re-rolled to be consistent with the last dev version
Comment #7
w.drupal commentedAlso the language was not taken into account
Comment #8
chrisckTested patch #7 and it works well on D9.1
Comment #9
chrisckSetting to RTBC.
Comment #10
volegerNeed to add tests for that case
Comment #11
junaidpv#7 works.
Comment #12
imclean commentedThis looks like an unrelated change so should probably be a separate issue.
Comment #13
nicklasmf commentedI 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.
Comment #14
imclean commentedTitle should reflect what's actually being proposed. "Incorrect" is a judgement call in this case, not a statement of fact.
Comment #15
imclean commentedIn addition to tests, this may need an upgrade path. The hash for each currently stored redirect will need to be updated.
Comment #18
decipheredMR opened from w.drupal's patch in #7, with the
setLanguage()call from #12 dropped and two kernel tests on top.#12: agreed, the language change is a separate concern. The pre-check now reads the language already set on the redirect entity, which is the value
Redirect::preSave()hashes. The lookup matches what the save writes, and which language a redirect is stored under does not change.#15: no upgrade path needed. Filefield Paths has never written the hash column.
Redirect::preSave()writes it on every save, from the source path, so every stored redirect already has the right hash. The bug was in the lookup only, which hashed the destination path and so never matched. Existing rows are found once this is merged.#10: the tests save the same redirect twice, once with the default language and once with
de, and assert one row. Both fail on the unique hash index without the fix.Comment #19
imclean commentedOf course, this is what led me to the problem in the first place. It has been a while since I looked at it.
Comment #22
deciphered