rom-sql icon indicating copy to clipboard operation
rom-sql copied to clipboard

Relation's dataset doesn't apply when the relation is used in a join

Open drqCode opened this issue 6 years ago • 2 comments

Hi! Here I make use of relation dataset to model different types of a task. In UserRepo I want a fetch all users having undone tasks and I want to load them with their undone tasks. The issue is that join with undone_tasks relation doesn't build a SQL query with the filters from undone_tasks dataset.

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :done_tasks
      has_many :undone_tasks
      has_many :tasks
    end
  end
end

class Tasks < ROM::Relation[:sql]
  schema(:tasks, infer: true) do
    associations { belongs_to :user }
  end
end

class UndoneTasks < ROM::Relation[:sql]
  schema(:tasks, infer: true) do
    associations { belongs_to :user }
  end

  dataset { where(done: false).order(:created_at).qualify }
end

class DoneTasks < ROM::Relation[:sql]
  schema(infer: true) do
    associations { belongs_to :user }
  end

  dataset { where(done: true).order(:created_at).qualify }
end


class UserRepo < ROM::Repository::Root[:users]
  def with_undone_tasks
    users
        .join(undone_tasks)
        .combine(:undone_tasks)
        .to_a
  end
end

drqCode avatar Jul 13 '18 11:07 drqCode

Try to define keys matching, something like join(undone_tasks, id: :user_id) I had similar issue a few months ago.

wmaciejak avatar Jul 25 '18 12:07 wmaciejak

Have the same issue with joins. Кузкщвгсфиду example can be minimised to:

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :undone_tasks
    end
  end
end

class UndoneTasks < ROM::Relation[:sql]
  schema(:tasks, infer: true) do
    associations { belongs_to :user }
  end

  dataset { where(done: false).order(:created_at).qualify }
end

users.join(undone_tasks) # dataset condition is ignored

Is there any workaround or recommended approach on how to handle such use-cases?

solenko avatar Dec 20 '21 10:12 solenko