Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachment unique constraints fail while uploading duplicate file #10

Open
fauno opened this issue Feb 28, 2021 · 2 comments
Open

Attachment unique constraints fail while uploading duplicate file #10

fauno opened this issue Feb 28, 2021 · 2 comments

Comments

@fauno
Copy link

fauno commented Feb 28, 2021

Hi, I'm very interested in using this gem and probably send patches. So far I've found this:

class Site < ActiveRecord::Base
  has_many_attached :files
end

Attaching a file twice throws a constraint error:

site = Site.create
site.files.attach io: File.open('/tmp/somefile.jpg'), filename: 'somefile.jpg')
site.files.attach io: File.open('/tmp/somefile.jpg'), filename: 'somefile.jpg')
=> ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: active_storage_attachments.record_type, active_storage_attachments.record_id, active_storage_attachments.name, active_storage_attachments.blob_id

Should IpfsService catch duplicate Attachments and remove them? Also getting the last Attachment without a query feels weird, wouldn't it be better to change it to something like Attachment.find_by(blob: new_blob)?

I'll prepare a PR after discussing, thanks!

@andrew
Copy link
Contributor

andrew commented Feb 28, 2021

I ran into a similar problem last summer: #6

@fauno
Copy link
Author

fauno commented Feb 28, 2021

I ran into a similar problem last summer: #6

Thanks! I saw it but this one happens after that, when the Attachment gets the existing Blob and just before discarding the new one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants