luckyframework / avram

A Crystal database wrapper for reading, writing, and migrating Postgres databases.
https://luckyframework.github.io/avram/
MIT License
165 stars 64 forks source link

Empty enumerable (Enumerable::EmptyError) #848

Open jwoertink opened 2 years ago

jwoertink commented 2 years ago

I'm not sure if this is an actual bug, or just a bad error, but this error happens if you pass in params to an operation and there's no permitted keys.

class SaveUser < User::SaveOperation
  permit_columns :name
end
# user:beepbeep=true
SaveUser.create(params) do |_,_|
end

Will need to track down where this is happening.

jwoertink commented 1 year ago

This is caused from this line

https://github.com/luckyframework/avram/blob/21345fd17faaa2a7b5d59777b86bbff67a860269/src/avram/save_operation.cr#L283

When we call insert, we use a RETURNING statement which should return a record, but I think if the insert fails, then nothing is returned, and we basically call [].first which raises this error. One option could be to rescue this error and re-raise a Avram::RecordInsertError or something...

Chances are, update has the same issue:

https://github.com/luckyframework/avram/blob/21345fd17faaa2a7b5d59777b86bbff67a860269/src/avram/save_operation.cr#L290