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

Wrong inferred name for a HABTM auxiliar table inside a schema #14824

Closed
emmx opened this issue Apr 20, 2014 · 9 comments
Closed

Wrong inferred name for a HABTM auxiliar table inside a schema #14824

emmx opened this issue Apr 20, 2014 · 9 comments
Assignees

Comments

@emmx
Copy link

emmx commented Apr 20, 2014

In PgSQL, when two models in a schema are associated through a has_and_belongs_to_many association, the name infered for the auxiliar table is wrong.

For instance:

class Music::Song < ActiveRecord::Base
  self.table_name = "music.songs"
  has_and_belongs_to_many :albums
end

class Music::Album < ActiveRecord::Base
  self.table_name = "music.albums"
  has_and_belongs_to_many :songs
end
Music::Song.includes(:albums).first
SELECT DISTINCT "music"."songs"."id", "music"."songs"."id" AS alias_0
FROM "music"."songs"
  LEFT OUTER JOIN "music"."albums_music" ON "music"."albums_music"."song_id" = "music"."songs"."id"
  LEFT OUTER JOIN "music"."albums" ON "music"."albums"."id" = "music"."albums_music"."album_id"
ORDER BY "music"."songs"."id" ASC
LIMIT 1

Here "music"."albums_music" should have been "music"."albums_songs", but somehow ActiveRecord is using the name of the first table plus the name schema.

It can be fixed adding join_table: "music.albums_songs" in the models, but I'm guessing it shouldn't be needed.

@estsauver
Copy link
Contributor

Hey @mparodi,

I think this is intentional behavior. See this chunk of docs:

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb#L1432

~Earl

@emmx
Copy link
Author

emmx commented Apr 20, 2014

"Unless the join table is explicitly specified as an option, it is guessed using the lexical order of the class names"

The class name here is "Album" and "Song", not "Music" (which is the name of the schema and module).

@estsauver
Copy link
Contributor

Sorry to misread. I'll try and put together a test case for this.

@emmx
Copy link
Author

emmx commented Apr 20, 2014

No problem!

Here is a migration for the example above:

execute "CREATE SCHEMA IF NOT EXISTS music"
execute "SET search_path TO music, public"

create_table :albums do |t|
  # ...
end

create_table :songs do |t|
  # ...
end

create_table :albums_songs do |t|
  t.belongs_to :album
  t.belongs_to :song
end
add_index :albums_songs, [:album_id, :song_id], :unique => true

By the way, let me know if there's a better way to work with schemas. I think using execute for this is kind of nasty actually :P

@estsauver
Copy link
Contributor

I've seen this script used by a number of people:

http://www.jonathanleighton.com/articles/2011/awesome-active-record-bug-reports/

Which makes it really easy to repro without having to setup a database for the test.

Your script should more then work. Thanks though!

@kassio
Copy link
Contributor

kassio commented Apr 25, 2014

I guess this is related with: #14709 and #14871

@eric-chahin
Copy link
Contributor

I do not believe that #14871 solves this issue. Using @kassio's fix, I still can reproduce this bug.

@eric-chahin
Copy link
Contributor

For reference, I am running this script: https://gist.github.com/eric-chahin/7eaba60b98ce20e068b9

@kassio
Copy link
Contributor

kassio commented Apr 30, 2014

@eric-chahin I see, the problem here goes beyond the namespace, I didn't get it at first. Thanks for the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants