File tree Expand file tree Collapse file tree 3 files changed +23
-1
lines changed
lib/active_record/connection_adapters Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -174,6 +174,10 @@ def supports_insert_returning?
174
174
mariadb? && database_version >= "10.5.0"
175
175
end
176
176
177
+ def return_value_after_insert? ( column ) # :nodoc:
178
+ supports_insert_returning? ? column . auto_populated? : column . auto_increment?
179
+ end
180
+
177
181
def get_advisory_lock ( lock_name , timeout = 0 ) # :nodoc:
178
182
query_value ( "SELECT GET_LOCK(#{ quote ( lock_name . to_s ) } , #{ timeout } )" ) == 1
179
183
end
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "cases/helper"
4
+ require "models/auto_id"
4
5
require "models/aircraft"
5
6
require "models/dashboard"
6
7
require "models/clothing_item"
@@ -38,6 +39,22 @@ def test_populates_non_primary_key_autoincremented_column
38
39
assert_not_nil topic . attributes [ "id" ]
39
40
end
40
41
42
+ def test_populates_autoincremented_id_pk_regardless_of_its_position_in_columns_list
43
+ auto_populated_column_names = AutoId . columns . select ( &:auto_populated? ) . map ( &:name )
44
+
45
+ # It's important we test a scenario where tables has more than one auto populated column
46
+ # and the first column is not the primary key. Otherwise it will be a regular test not asserting this special case.
47
+ assert auto_populated_column_names . size > 1
48
+ assert_not_equal AutoId . primary_key , auto_populated_column_names . first
49
+
50
+ record = AutoId . create!
51
+ last_id = AutoId . last . id
52
+
53
+ assert_not_nil last_id
54
+ assert last_id > 0
55
+ assert_equal last_id , record . id
56
+ end
57
+
41
58
def test_populates_non_primary_key_autoincremented_column_for_a_cpk_model
42
59
order = Cpk ::Order . create ( shop_id : 111_222 )
43
60
Original file line number Diff line number Diff line change 105
105
end
106
106
107
107
create_table :auto_id_tests , force : true , id : false do |t |
108
- t . primary_key :auto_id
109
108
t . integer :value
109
+ t . timestamp :published_at , default : -> { "CURRENT_TIMESTAMP" }
110
+ t . primary_key :auto_id
110
111
end
111
112
112
113
create_table :binaries , force : true do |t |
You can’t perform that action at this time.
0 commit comments