luckyframework / avram

A Crystal database wrapper for reading, writing, and migrating Postgres databases.

Home Page:https://luckyframework.github.io/avram/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Avoid this query pattern and avoid crashing your app

jwoertink opened this issue · comments

I've done this before, and just did it again which leads to this fun error:

image

When you have a query object, and you create a method named the same as a column of that model, you'll blow things up if you try to order on that "method".

class TaskQuery < Task::BaseQuery
  def active
    active(true)
  end
end

# returns all tasks that are active. Works great!
TaskQuery.new.active

# Try to sort where the active ones are first, but cause infinite recursion and tank your system
TaskQuery.new.active.asc_order

Maybe we can somehow catch that if you define a method named the same as a column name, it throws a compile-time error? I think there's a method_added macro https://crystal-lang.org/reference/1.7/syntax_and_semantics/macros/hooks.html