Use of projection dsl is not available in Relation#exclude

Open
#412 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
ruby, sql
Domain
databases

Research direction

Start at Relation#exclude and the projection DSL shown in the reproduction, then reproduce the query to trace where the block is evaluated. Add coverage for the expected NOT (1 = ANY(...)) SQL and confirm the projection and existing conditions remain unchanged.

Written by the indexing model from the issue text.

Description

bug help wanted

Describe the bug

It I try to use the projection dsl in combination with #exlude...

mentions
  .select { integer::count(issue_id).as(:unit_mention_count) }
  .where(unit_id: 3, read: false)
  .exclude { user_ids.any(1) }

...I get the following error:

NoMethodError:
  undefined method `any' for #<Sequel::SQL::Identifier @value=>:user_ids>
         .exclude { user_ids.any(1) }

Expected behavior

SELECT COUNT(\"mentions\".\"issue_id\") AS \"unit_mention_count\" FROM \"mentions\" WHERE ((\"unit_id\" = 3) AND (\"read\" IS FALSE) AND NOT (1 = ANY(\"mentions\".\"user_ids\")))

Workaround

Overwrite #exclude at relation

class Mentions < ROM::Relation[:sql]
  schema(:mentions, infer: true) do
  end

  def exclude(*args, &block)
    if block
      exclude(*args).exclude(schema.canonical.restriction(&block))
    elsif args.size == 1 && args[0].is_a?(Hash)
      new(dataset.exclude(coerce_conditions(args[0])))
    elsif !args.empty?
      new(dataset.exclude(*args))
    else
      self
    end
  end
end

WDYT? I could provide a PR.

Dominant language
Ruby
Stars
220
Forks
97
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rom-rb/rom-sql

All issues in rom-rb/rom-sql

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.