Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for has_many-through with a join table that "has many targets" #194

Open
v-kolesnikov opened this issue Apr 30, 2017 · 3 comments
Open

Comments

@v-kolesnikov
Copy link
Collaborator

require 'rom'
require 'sqlite3'

rom = ROM.container(:sql, 'sqlite::memory') do |conf|
  conf.default.create_table(:bookings) do
    primary_key :booking_id
  end

  conf.default.create_table(:tickets) do
    primary_key :ticket_id
    column :booking_id, Integer
  end

  conf.default.create_table(:passes) do
    primary_key :pass_id
    column :ticket_id, Integer
  end

  conf.relation(:bookings) do
    schema(infer: true) do
      associations do
        has_many :tickets
        has_many :passes, through: :tickets
      end
    end

    def with_passes
      join(passes)
    end
  end

  conf.relation(:tickets) do
    schema(infer: true) do
      associations do
        belongs_to :booking
        has_many :passes
      end
    end
  end

  conf.relation(:passes) do
    schema(infer: true) do
      associations do
        belongs_to :tickets
      end
    end
  end
end

joined = rom.relation(:bookings).join(rom.relation(:passes))

Expected SQL:

SELECT `bookings`.`booking_id`
FROM `bookings`
INNER JOIN `tickets` ON (`bookings`.`booking_id` = `tickets`.`booking_id`)
INNER JOIN `passes` ON (`tickets`.`ticket_id` = `passes`.`pass_id`)
ORDER BY `bookings`.`booking_id`

Actual SQL:

SELECT `bookings`.`booking_id`
FROM `bookings`
INNER JOIN `tickets` ON (`bookings`.`booking_id` = `tickets`.`booking_id`)
INNER JOIN `passes` ON (`bookings`.`booking_id` = `tickets`.`booking_id`)
ORDER BY `bookings`.`booking_id`
@solnic solnic added the bug label Apr 30, 2017
@solnic solnic added this to the v1.3.0 milestone Apr 30, 2017
@solnic
Copy link
Member

solnic commented Apr 30, 2017

OK I'm not sure if this is a bug after all. tickets is not a typical join table (it would be if it had a belongs_to :passes and has_many-through is designed to work with join tables exclusively. I'm gonna put this one on hold for now.

@solnic solnic added discussion and removed bug labels Apr 30, 2017
@solnic
Copy link
Member

solnic commented Apr 30, 2017

@flash-gordon wdyt about supporting has-many-through when join table is not a typical join table that has both FKs, and instead it 'has-many' targets?

@solnic solnic modified the milestones: v2.0.0, v1.3.0 Apr 30, 2017
@solnic solnic changed the title Broken has_many_many join Support for has_many-through with a join table that "has many targets" Apr 30, 2017
@flash-gordon
Copy link
Member

@solnic the actual problem is that it's not obvious how to join a relation through another relation

this doesn't work too:

joined = rom.relation(:bookings).join(rom.relation(:tickets)).join(rom.relation(:passes))
 :passes doesn't exist in ROM::AssociationSet registry (ROM::Registry::ElementNotFoundError)

@solnic solnic removed this from the v2.0.0 milestone Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants