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

Wrap plus combine fails

Open mrship opened this issue 6 years ago • 0 comments

I have an issue where using wrap and combine together causes the error:

 wrong number of arguments (given 3, expected 2) (ArgumentError)

However, if I use two combines, then it works as expected. A repro script is inlined below. This is slightly complicated because the relationship for manager is on the same table, but I have this elsewhere as well with more "standard" table relationships.


#!/usr/bin/env ruby
require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'rom'
  gem 'rom-sql'
  gem 'rom-repository'
  gem 'dry-types'
  gem 'sqlite3'
end

require 'rom-sql'
require 'rom-repository'
require 'rom/transformer'
require 'dry-types'

module Types
  include Dry::Types.module
end

rom = ROM.container(:sql, 'sqlite::memory') do |c|
  c.gateways[:default].create_table :active_directory do
    column :mail, String
    column :name, String
    column :gwereportingto, String
  end

  c.gateways[:default].create_table :hierarchies do
    column :employee, String
    column :level, Integer
    column :no_of_reportees, String
  end

  c.gateways[:default].use_logger(Logger.new($stdout))

  c.relation(:hierarchies) do
    schema(:hierarchies, infer: false) do
      attribute :employee, Types::String.meta(primary_key: true)
      attribute :level, Types::Int
      attribute :no_of_reportees, Types::Int
    end
  end

  c.relation(:colleagues) do
    schema(:active_directory, infer: false, as: :colleagues) do
      attribute :mail, Types::String.meta(primary_key: true)
      attribute :name, Types::String
      attribute :gwereportingto, Types::String

      associations do
        belongs_to :hierarchy, foreign_key: :mail
        belongs_to :active_directory, relation: :colleagues, as: :manager, foreign_key: :gwereportingto
      end
    end
  end
end

jane = rom.relations[:colleagues].insert(mail: "[email protected]", name: "Jane")
fred = rom.relations[:colleagues].insert(mail: "[email protected]", name: "Fred", gwereportingto: "[email protected]")
rom.relations[:hierarchies].insert(employee: "[email protected]", no_of_reportees: 1, level: 2)

class ColleagueRepo < ROM::Repository[:colleagues]
  def wrap_and_combine
    colleagues
      .wrap(:hierarchies)
      .combine(:manager)
      .where(mail: "[email protected]")
      .where(level: 2)
      .one
  end

  def two_combines
    colleagues
      .join(:hierarchies)
      .combine(:hierarchy, :manager)
      .where(mail: "[email protected]")
      .where(level: 2)
      .one
  end
end

repo = ColleagueRepo.new(rom)

puts "------\n"
puts repo.two_combines.inspect
puts "------\n"
puts repo.wrap_and_combine.inspect

#<ROM::Struct::Colleague mail="[email protected]" name="Fred" gwereportingto="[email protected]" hierarchy=#<ROM::Struct::Hierarchy employee="[email protected]" level=2 no_of_reportees="1"> manager=#<ROM::Struct::Manager mail="[email protected]" name="Jane" gwereportingto=nil>>
------
/Users/shipmana/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rom-core-4.2.1/lib/rom/relation/combined.rb:21:in `new': wrong number of arguments (given 3, expected 2) (ArgumentError)

mrship avatar Jul 12 '18 08:07 mrship