Merge pull request #14965 from eric-chahin/issue_14824
Fixed the inferred table name of a HABTM auxiliar
This commit is contained in:
commit
f9860fcbe2
@ -1,3 +1,9 @@
|
||||
* Fixed the inferred table name of a HABTM auxiliar table inside a schema.
|
||||
|
||||
Fixes #14824
|
||||
|
||||
*Eric Chahin*
|
||||
|
||||
* Remove unused `:timestamp` type. Transparently alias it to `:datetime`
|
||||
in all cases. Fixes inconsistencies when column types are sent outside of
|
||||
`ActiveRecord`, such as for XML Serialization.
|
||||
|
@ -11,7 +11,7 @@ def initialize(lhs_class, rhs_class_name)
|
||||
end
|
||||
|
||||
def join_table
|
||||
@join_table ||= [@lhs_class.table_name, klass.table_name].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_")
|
||||
@join_table ||= [@lhs_class.table_name, klass.table_name].sort.join("\0").gsub(/^(.*[._])(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_")
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -50,6 +50,16 @@ class Thing5 < ActiveRecord::Base
|
||||
self.table_name = 'things'
|
||||
end
|
||||
|
||||
class Song < ActiveRecord::Base
|
||||
self.table_name = "music.songs"
|
||||
has_and_belongs_to_many :albums
|
||||
end
|
||||
|
||||
class Album < ActiveRecord::Base
|
||||
self.table_name = "music.albums"
|
||||
has_and_belongs_to_many :songs
|
||||
end
|
||||
|
||||
def setup
|
||||
@connection = ActiveRecord::Base.connection
|
||||
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
|
||||
@ -109,6 +119,22 @@ def test_drop_schema
|
||||
assert !@connection.schema_names.include?("test_schema3")
|
||||
end
|
||||
|
||||
def test_habtm_table_name_with_schema
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
DROP SCHEMA IF EXISTS music CASCADE;
|
||||
CREATE SCHEMA music;
|
||||
CREATE TABLE music.albums (id serial primary key);
|
||||
CREATE TABLE music.songs (id serial primary key);
|
||||
CREATE TABLE music.albums_songs (album_id integer, song_id integer);
|
||||
SQL
|
||||
|
||||
song = Song.create
|
||||
album = Album.create
|
||||
assert_equal song, Song.includes(:albums).references(:albums).first
|
||||
ensure
|
||||
ActiveRecord::Base.connection.execute "DROP SCHEMA music CASCADE;"
|
||||
end
|
||||
|
||||
def test_raise_drop_schema_with_nonexisting_schema
|
||||
assert_raises(ActiveRecord::StatementInvalid) do
|
||||
@connection.drop_schema "test_schema3"
|
||||
|
Loading…
Reference in New Issue
Block a user