Jsonb field for attachment data with default value causes error

Hello,

Migrating from shrine 2.x to 3.x has caused an error when saving a model with an attachment. I can’t seem to find any reference on the migration guide to what could cause this.

Example code:

class ModelUploader < Shrine
end
class Model < ApplicationRecord
  include ModelUploader::Attachment.new(:attachment)
  ...
end

When trying to create a new record:

...

model = Model.new(
  attachment: File.open(file.path, "r"),
  att1: att1,
  att2: att2,
)

...

I get this error:

Shrine::Error:
        {} isn't valid uploaded file data

After debugging and following the source code, I arrived to the conclusion that the “problem” was that the attachment data jsonb column definition was using a default value and not allowing nulls:

create_table "models", force: :cascade do |t|
  t.jsonb "attachment_data", default: {}, null: false
  ...
end

Creating a migration and changing the column definition to:

create_table "models", force: :cascade do |t|
  t.jsonb "attachment_data"
  ...
end

Seems to fix the problem using 3.x. In shrine 2.x the first column definition didn’t trigger the error at all.

Am I doing something wrong or is this a bug (most probably not, seeing that I haven’t seen anybody reporting this problem)

Thanks in advance!

Yes, Shrine 3.x doesn’t support JSONB columns having {} as the default value. Shrine 2.x will support it when you’re using versions plugin, but that functionality wasn’t intentional.

I don’t think this should be supported in Shrine, as I believe it’s always better to default to NULL than to {}, as that makes it clear that there is no attached file.

1 Like

Alright, good to know!

Thanks for the reply @janko

SOLVED!