mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #18880 from mudge/timestamp-index-bug
Fix missing index when using timestamps with index
This commit is contained in:
commit
6f37d7e117
3 changed files with 20 additions and 0 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
* Fix missing index when using `timestamps` with the `index` option.
|
||||||
|
|
||||||
|
The `index` option used with `timestamps` should be passed to both
|
||||||
|
`column` definitions for `created_at` and `updated_at` rather than just
|
||||||
|
the first.
|
||||||
|
|
||||||
|
*Paul Mucur*
|
||||||
|
|
||||||
* Rename `:class` to `:anonymous_class` in association options.
|
* Rename `:class` to `:anonymous_class` in association options.
|
||||||
|
|
||||||
Fixes #19659.
|
Fixes #19659.
|
||||||
|
|
|
@ -359,6 +359,7 @@ module ActiveRecord
|
||||||
def column(name, type, options = {})
|
def column(name, type, options = {})
|
||||||
name = name.to_s
|
name = name.to_s
|
||||||
type = type.to_sym
|
type = type.to_sym
|
||||||
|
options = options.dup
|
||||||
|
|
||||||
if @columns_hash[name] && @columns_hash[name].primary_key?
|
if @columns_hash[name] && @columns_hash[name].primary_key?
|
||||||
raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table."
|
raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table."
|
||||||
|
|
|
@ -446,6 +446,17 @@ class TimestampTest < ActiveRecord::TestCase
|
||||||
toy = Toy.first
|
toy = Toy.first
|
||||||
assert_equal [:created_at, :updated_at], toy.send(:all_timestamp_attributes_in_model)
|
assert_equal [:created_at, :updated_at], toy.send(:all_timestamp_attributes_in_model)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_index_is_created_for_both_timestamps
|
||||||
|
ActiveRecord::Base.connection.create_table(:foos, force: true) do |t|
|
||||||
|
t.timestamps(:foos, null: true, index: true)
|
||||||
|
end
|
||||||
|
|
||||||
|
indexes = ActiveRecord::Base.connection.indexes('foos')
|
||||||
|
assert_equal ['created_at', 'updated_at'], indexes.flat_map(&:columns).sort
|
||||||
|
ensure
|
||||||
|
ActiveRecord::Base.connection.drop_table(:foos)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class TimestampsWithoutTransactionTest < ActiveRecord::TestCase
|
class TimestampsWithoutTransactionTest < ActiveRecord::TestCase
|
||||||
|
|
Loading…
Reference in a new issue