@@ -1280,7 +1280,9 @@ defmodule Ecto.Migration do
12801280 the example above), or `nil`.
12811281 * `:type` - The foreign key type, which defaults to `:bigserial`.
12821282 * `:on_delete` - What to do if the referenced entry is deleted. May be
1283- `:nothing` (default), `:delete_all`, `:nilify_all`, or `:restrict`.
1283+ `:nothing` (default), `:delete_all`, `:nilify_all`, `{:nilify, columns}`,
1284+ or `:restrict`. `{:nilify, columns}` expects a list of atoms for `columns`
1285+ and is not supported by all databases.
12841286 * `:on_update` - What to do if the referenced entry is updated. May be
12851287 `:nothing` (default), `:update_all`, `:nilify_all`, or `:restrict`.
12861288 * `:validate` - Whether or not to validate the foreign key constraint on
@@ -1302,14 +1304,8 @@ defmodule Ecto.Migration do
13021304 def references ( table , opts ) when is_binary ( table ) and is_list ( opts ) do
13031305 opts = Keyword . merge ( foreign_key_repo_opts ( ) , opts )
13041306 reference = struct ( % Reference { table: table } , opts )
1305-
1306- unless reference . on_delete in [ :nothing , :delete_all , :nilify_all , :restrict ] do
1307- raise ArgumentError , "unknown :on_delete value: #{ inspect reference . on_delete } "
1308- end
1309-
1310- unless reference . on_update in [ :nothing , :update_all , :nilify_all , :restrict ] do
1311- raise ArgumentError , "unknown :on_update value: #{ inspect reference . on_update } "
1312- end
1307+ check_on_delete! ( reference . on_delete )
1308+ check_on_update! ( reference . on_update )
13131309
13141310 reference
13151311 end
@@ -1323,6 +1319,31 @@ defmodule Ecto.Migration do
13231319 |> Keyword . merge ( Runner . repo_config ( :migration_foreign_key , [ ] ) )
13241320 end
13251321
1322+ defp check_on_delete! ( on_delete )
1323+ when on_delete in [ :nothing , :delete_all , :nilify_all , :restrict ] ,
1324+ do: :ok
1325+
1326+ defp check_on_delete! ( { :nilify , columns } ) when is_list ( columns ) do
1327+ unless Enum . all? ( columns , & is_atom / 1 ) do
1328+ raise ArgumentError ,
1329+ "expected `columns` in `{:nilify, columns}` to be a list of atoms, got: #{ inspect columns } "
1330+ end
1331+
1332+ :ok
1333+ end
1334+
1335+ defp check_on_delete! ( on_delete ) do
1336+ raise ArgumentError , "unknown :on_delete value: #{ inspect ( on_delete ) } "
1337+ end
1338+
1339+ defp check_on_update! ( on_update )
1340+ when on_update in [ :nothing , :update_all , :nilify_all , :restrict ] ,
1341+ do: :ok
1342+
1343+ defp check_on_update! ( on_update ) do
1344+ raise ArgumentError , "unknown :on_update value: #{ inspect ( on_update ) } "
1345+ end
1346+
13261347 @ doc ~S"""
13271348 Defines a constraint (either a check constraint or an exclusion constraint)
13281349 to be evaluated by the database when a row is inserted or updated.
0 commit comments