Skip to content

Commit a0fe7b7

Browse files
committed
Merge pull request rails#18880 from mudge/timestamp-index-bug
Fix missing index when using timestamps with index
1 parent deb8aaf commit a0fe7b7

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Fix missing index when using `timestamps` with the `index` option.
2+
3+
The `index` option used with `timestamps` should be passed to both
4+
`column` definitions for `created_at` and `updated_at` rather than just
5+
the first.
6+
7+
*Paul Mucur*
8+
19
* Rename `:class` to `:anonymous_class` in association options.
210

311
Fixes #19659.

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def [](name)
256256
def column(name, type, options = {})
257257
name = name.to_s
258258
type = type.to_sym
259+
options = options.dup
259260

260261
if @columns_hash[name] && @columns_hash[name].primary_key?
261262
raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table."

activerecord/test/cases/timestamp_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,17 @@ def test_all_timestamp_attributes_in_model
425425
toy = Toy.first
426426
assert_equal [:created_at, :updated_at], toy.send(:all_timestamp_attributes_in_model)
427427
end
428+
429+
def test_index_is_created_for_both_timestamps
430+
ActiveRecord::Base.connection.create_table(:foos, force: true) do |t|
431+
t.timestamps(:foos, null: true, index: true)
432+
end
433+
434+
indexes = ActiveRecord::Base.connection.indexes('foos')
435+
assert_equal ['created_at', 'updated_at'], indexes.flat_map(&:columns).sort
436+
ensure
437+
ActiveRecord::Base.connection.drop_table(:foos)
438+
end
428439
end
429440

430441
class TimestampsWithoutTransactionTest < ActiveRecord::TestCase

0 commit comments

Comments
 (0)