Skip to content

Commit 609d4c1

Browse files
committed
Update docs and add a warning block for this function
1 parent 79e6479 commit 609d4c1

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

lib/ecto/migration.ex

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,13 +1273,44 @@ defmodule Ecto.Migration do
12731273
12741274
See `add/3` for more information on supported types.
12751275
1276-
If you want to modify a column without changing its type,
1277-
such as adding or dropping a null constraints, consider using
1278-
the `execute/2` command with the relevant SQL command instead
1279-
of `modify/3`, if supported by your database. This may avoid
1280-
redundant type updates and be more efficient, as an unnecessary
1281-
type update can lock the table, even if the type actually
1282-
doesn't change.
1276+
> #### Modifying a column without changing its type {: .warning}
1277+
>
1278+
> If you want to modify a column without changing its type,
1279+
> such as adding or dropping a null constraints, consider using
1280+
> the `execute/2` command with the relevant SQL command instead
1281+
> of `modify/3`, if supported by your database. This may avoid
1282+
> redundant type updates and be more efficient, as an unnecessary
1283+
> type update can lock the table, even if the type actually
1284+
> doesn't change.
1285+
>
1286+
> We have considered changing the column type even when it is not needed
1287+
> could lead to undesirable locks, that's why, at least in the PostgreSQL
1288+
> adapter, if you provide the option `:from`, and the type matches, we
1289+
> will avoid changing the type.
1290+
>
1291+
> Examples
1292+
>
1293+
> # modify column with rollback options
1294+
> alter table("posts") do
1295+
> modify :title, :text, null: false, from: {:text, null: true}
1296+
> end
1297+
>
1298+
> # adding a new foreign key constraint
1299+
> alter table("posts") do
1300+
> modify :author_id, references(:authors, type: :id), from: :id
1301+
> end
1302+
>
1303+
> # Modify the :on_delete option of an existing foreign key
1304+
> alter table("comments") do
1305+
> modify :post_id, references(:posts, on_delete: :delete_all),
1306+
> from: references(:posts, on_delete: :nothing)
1307+
> end
1308+
>
1309+
>
1310+
> The previous syntax will offer two benefits, at least in the PostgreSQL adapter,
1311+
> the migration is reversible and if the column type remains the same, the column
1312+
> type update will be skipped.
1313+
12831314
12841315
## Examples
12851316

0 commit comments

Comments
 (0)