fk: raise for invalid :on_update / :on_delete values

This commit is contained in:
Yves Senn 2014-06-20 08:56:30 +02:00
parent 31e4c19331
commit 8550ba307d
2 changed files with 18 additions and 3 deletions

View File

@ -104,9 +104,14 @@ FOREIGN KEY (#{quote_column_name(o.column)})
def action_sql(action, dependency)
case dependency
when :nullify then "ON #{action} SET NULL"
when :cascade then "ON #{action} CASCADE"
when :restrict then "ON #{action} RESTRICT"
when :nullify then "ON #{action} SET NULL"
when :cascade then "ON #{action} CASCADE"
when :restrict then "ON #{action} RESTRICT"
else
raise ArgumentError, <<-MSG
'#{dependency}' is not supported for :on_update or :on_delete.
Supported values are: :nullify, :cascade, :restrict
MSG
end
end
end

View File

@ -126,6 +126,16 @@ module ActiveRecord
assert_equal :nullify, fk.on_delete
end
def test_on_update_and_on_delete_raises_with_invalid_values
assert_raises ArgumentError do
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :invalid
end
assert_raises ArgumentError do
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_update: :invalid
end
end
def test_add_foreign_key_with_on_update
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_update: :nullify