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
Comments
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 |
"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). |
Sorry to misread. I'll try and put together a test case for this. |
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 |
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! |
For reference, I am running this script: https://gist.github.com/eric-chahin/7eaba60b98ce20e068b9 |
@eric-chahin I see, the problem here goes beyond the namespace, I didn't get it at first. Thanks for the tests. |
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:
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.
The text was updated successfully, but these errors were encountered: