avram icon indicating copy to clipboard operation
avram copied to clipboard

SaveOperation fails to compile if using enum column defined within model

Open matthewmcgarvey opened this issue 3 years ago • 2 comments

I have a class Order and a column called state where the value is an enum OrderState. I originally had the enum defined within the model but upon creating and trying to use a SaveOperation it failed to compile with this error message

web          | Showing last frame. Use --error-trace for full trace.
web          | There was a problem expanding macro 'generate_create'
web          |
web          | Code in macro 'generate_save_methods'
web          |
web          |  2 | generate_create("id : Int64 | Avram::Nothing = Avram::Nothing.new,\ncreated_at : Time | Avram::Nothing = Avram::Nothing.new,\nupdated_at : Time | Avram::Nothing = Avram::Nothing.new,\nstate : OrderState | Avram::Nothing = Avram::Nothing.new,\nguest_token : String | Avram::Nothing | Nil = Avram::Nothing.new,\nuser_id : User::PrimaryKeyType | Avram::Nothing | Nil = Avram::Nothing.new,\n", "id: id,\ncreated_at: created_at,\nupdated_at: updated_at,\nstate: state,\nguest_token: guest_token,\nuser_id: user_id,\n", with_params: true, with_bang: true)
web          |      ^
web          | Called macro defined in lib/avram/src/avram/needy_initializer_and_save_methods.cr:96:3
web          |
web          |  96 | macro generate_create(attribute_method_args, attribute_params, with_params, with_bang)
web          |
web          | Which expanded to:
web          |
web          |  > 13 | created_at : Time | Avram::Nothing = Avram::Nothing.new,
web          |  > 14 | updated_at : Time | Avram::Nothing = Avram::Nothing.new,
web          |  > 15 | state : OrderState | Avram::Nothing = Avram::Nothing.new,
web          |                 ^---------
web          | Error: undefined constant OrderState

This is from version 1.0.0-rc1

matthewmcgarvey avatar Oct 25 '22 23:10 matthewmcgarvey

Just in case it becomes a factor, I defined the enum below my table block. I don't know how it could be a problem, but I have no ideas on the problem.

matthewmcgarvey avatar Oct 25 '22 23:10 matthewmcgarvey

My guess is you had?:

column : state = OrderState

Try the fully qualified name:

column : state = Order::OrderState # <=

...and move the enum definition above the .table macro.

akadusei avatar Dec 15 '22 11:12 akadusei