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

Joining tables with a block condition fails with aliased relation #369

Closed
slicedpan opened this issue Feb 26, 2020 · 2 comments · Fixed by #372
Closed

Joining tables with a block condition fails with aliased relation #369

slicedpan opened this issue Feb 26, 2020 · 2 comments · Fixed by #372
Labels

Comments

@slicedpan
Copy link

Describe the bug

If you have a relation that corresponds to a table with a different name than the table, then joining will fail because the JOIN query will try to join to a table with the relation name, not the table name. I'm fairly sure this only happens when you pass a block to the join method to set the join conditions.

To Reproduce

The following snippet will exhibit this bug:

require "rom"

rom = ROM.container(:sql, 'sqlite::memory') do |conf|
  conf.default.create_table(:users) do
    primary_key :id
    column :name, String, null: false
  end

  conf.default.create_table(:my_tasks) do
    primary_key :id
    column :name, String, null: false
    foreign_key :user_id, :users, null: false
  end

  conf.relation(:users) do
    schema(infer: true)
  end

  conf.relation(:tasks) do
    schema(:my_tasks, infer: true) do
      associations do
        belongs_to(:user)
      end
    end
  end
end

rom.relations[:users].join(rom.relations[:tasks]) do |users:, tasks:|
  tasks[:user_id].is(users[:id])
end.to_a

If you inspect the query before calling to_a you will see the following:

SELECT `users`.`id`, `users`.`name` FROM `users` INNER JOIN `tasks` ON (`my_tasks`.`user_id` = `users`.`id`) ORDER BY `users`.`id`

As you can see, the query is trying to join a table named tasks and not my_tasks as you might expect.

Expected behavior

The JOIN should reference the correct table name when supplying a block with join conditions.

Your environment

  • Affects my production application: NO
  • Ruby version: ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-linux]
  • OS: Ubuntu 16.04
@slicedpan slicedpan added the bug label Feb 26, 2020
@abrthel
Copy link
Contributor

abrthel commented Mar 3, 2020

Everyone ok with changing
https://github.com/rom-rb/rom-sql/blob/master/lib/rom/sql/relation/reading.rb#L1097

other.name.to_sym -> other.name.dataset.to_sym

@solnic
Copy link
Member

solnic commented Mar 3, 2020

Everyone ok with changing
https://github.com/rom-rb/rom-sql/blob/master/lib/rom/sql/relation/reading.rb#L1097
other.name.to_sym -> other.name.dataset.to_sym

@abrthel yep! that's clearly a bug with a simple fix like you suggested

solnic added a commit that referenced this issue Mar 4, 2020
relation#join block dsl should use dataset name not alias

[changelog]

version: "unreleased"
date:
fixed: "`Relation#join` no longer crashes when aliased relation is used with a block (issue #369) (@abrthel)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants