Skip to content

Commit e8bf578

Browse files
Raise when both :force and :if_not_exists provided to create_table
Co-authored-by: Allison Phillips <[email protected]>
1 parent bad7ff1 commit e8bf578

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ def primary_key(table_name)
293293
def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **options, &block)
294294
validate_create_table_options!(options)
295295
validate_table_length!(table_name) unless options[:_uses_legacy_table_name]
296+
297+
if force && options.key?(:if_not_exists)
298+
raise ArgumentError, "Options `:force` and `:if_not_exists` cannot be used simultaneously."
299+
end
300+
296301
td = build_create_table_definition(table_name, id: id, primary_key: primary_key, force: force, **options, &block)
297302

298303
if force

activerecord/test/cases/migration_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ def test_create_table_raises_for_long_table_names
199199
connection.drop_table short_name, if_exists: true
200200
end
201201

202+
def test_create_table_with_force_and_if_not_exists
203+
connection = Person.lease_connection
204+
assert_raises(ArgumentError, match: /Options `:force` and `:if_not_exists` cannot be used simultaneously/) do
205+
connection.create_table(:testings, force: true, if_not_exists: true)
206+
end
207+
end
208+
202209
def test_create_table_with_indexes_and_if_not_exists_true
203210
connection = Person.lease_connection
204211
connection.create_table :testings, force: true do |t|

0 commit comments

Comments
 (0)