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

create_join_table should respect references column type #28217

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,16 @@ def create_table(table_name, comment: nil, **options)
# part_id int NOT NULL,
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8
#
def create_join_table(table_1, table_2, options = {})
def create_join_table(table_1, table_2, column_options: {}, **options)
join_table_name = find_join_table_name(table_1, table_2, options)

column_options = options.delete(:column_options) || {}
column_options.reverse_merge!(null: false)
type = column_options.delete(:type) || :integer
column_options.reverse_merge!(null: false, index: false)

t1_column, t2_column = [table_1, table_2].map { |t| t.to_s.singularize.foreign_key }
t1_ref, t2_ref = [table_1, table_2].map { |t| t.to_s.singularize }

create_table(join_table_name, options.merge!(id: false)) do |td|
td.send type, t1_column, column_options
td.send type, t2_column, column_options
td.references t1_ref, column_options
td.references t2_ref, column_options
yield td if block_given?
end
end
Expand Down
13 changes: 12 additions & 1 deletion activerecord/test/cases/migration/create_join_table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setup

teardown do
%w(artists_musics musics_videos catalog).each do |table_name|
connection.drop_table table_name if connection.table_exists?(table_name)
connection.drop_table table_name, if_exists: true
end
end

Expand Down Expand Up @@ -78,6 +78,17 @@ def test_create_join_table_with_index
assert_equal [%w(artist_id music_id)], connection.indexes(:artists_musics).map(&:columns)
end

def test_create_join_table_respects_reference_key_type
connection.create_join_table :artists, :musics do |t|
t.references :video

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

references key type is the same as primary_key type and it depends on an adapter. bigint for mysql2 and postgresql, integer for sqlite3 (related #27334). If primary_key is bigint, create_join_table should create bigint columns.

end

artist_id, music_id, video_id = connection.columns(:artists_musics).sort_by(&:name)

assert_equal video_id.sql_type, artist_id.sql_type
assert_equal video_id.sql_type, music_id.sql_type
end

def test_drop_join_table
connection.create_join_table :artists, :musics
connection.drop_join_table :artists, :musics
Expand Down