Skip to content

Commit 4f0c990

Browse files
authored
Pass allow_stale opt to assocs (#4528)
1 parent 0d2a56e commit 4f0c990

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

lib/ecto/repo.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,9 +1660,9 @@ defmodule Ecto.Repo do
16601660
* `:stale_error_message` - The message to add to the configured
16611661
`:stale_error_field` when stale errors happen, defaults to "is stale".
16621662
1663-
* `:allow_stale` - Doesn't error if insert is stale. Defaults to `false`.
1663+
* `:allow_stale` - Doesn't error when structs are stale. Defaults to `false`.
16641664
This may happen if there are rules or triggers in the database that
1665-
rejects the insert operation.
1665+
rejects the insert operation. This option cascades to associations.
16661666
16671667
See the ["Shared options"](#module-shared-options) section at the module
16681668
documentation for more options.
@@ -1863,7 +1863,7 @@ defmodule Ecto.Repo do
18631863
* `:allow_stale` - Doesn't error if update is stale. Defaults to `false`.
18641864
This may happen if the struct has been deleted from the database before
18651865
the update or if there is a rule or a trigger on the database that rejects
1866-
the update operation.
1866+
the update operation. This option cascades to associations.
18671867
18681868
See the ["Shared options"](#module-shared-options) section at the module
18691869
documentation for more options.
@@ -1909,8 +1909,8 @@ defmodule Ecto.Repo do
19091909
* `:stale_error_message` - The message to add to the configured
19101910
`:stale_error_field` when stale errors happen, defaults to "is stale".
19111911
Only applies to updates.
1912-
* `:allow_stale` - Doesn't error if delete is stale. Defaults to `false`.
1913-
Only applies to updates.
1912+
* `:allow_stale` - Doesn't error when structs are stale. Defaults to `false`.
1913+
This option cascades to associations.
19141914
19151915
See the ["Shared options"](#module-shared-options) section at the module
19161916
documentation for more options.

lib/ecto/repo/schema.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ defmodule Ecto.Repo.Schema do
977977
defp assoc_opts([], _opts), do: []
978978

979979
defp assoc_opts(_assocs, opts) do
980-
Keyword.take(opts, [:timeout, :log, :telemetry_event, :prefix])
980+
Keyword.take(opts, [:timeout, :log, :telemetry_event, :prefix, :allow_stale])
981981
end
982982

983983
defp process_parents(changeset, user_changeset, assocs, reset_assocs, adapter, opts) do

test/ecto/repo_test.exs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ defmodule Ecto.RepoTest do
4949

5050
schema "my_schema_child" do
5151
field :a, :string
52-
belongs_to :my_schema, MySchemaNoPK, references: :n, foreign_key: :n
52+
belongs_to :my_schema, MySchema
53+
belongs_to :my_schema_no_pk, MySchemaNoPK, references: :n, foreign_key: :n
5354
end
5455

5556
def changeset(struct, params) do
@@ -1130,6 +1131,23 @@ defmodule Ecto.RepoTest do
11301131
assert {:ok, _} = TestRepo.delete(stale, allow_stale: true)
11311132
end
11321133

1134+
test "insert, update allows stale children with :allow_stale option" do
1135+
child_schema =
1136+
%MySchemaChild{a: "one"}
1137+
1138+
stale =
1139+
put_in(child_schema.__meta__.context, {:error, :stale})
1140+
|> Ecto.Changeset.change()
1141+
1142+
changeset =
1143+
%MySchema{id: 1}
1144+
|> Ecto.Changeset.change()
1145+
|> Ecto.Changeset.put_assoc(:children, [stale])
1146+
1147+
assert {:ok, _} = TestRepo.insert(changeset, allow_stale: true)
1148+
assert {:ok, _} = TestRepo.update(changeset, allow_stale: true)
1149+
end
1150+
11331151
test "insert and delete sets schema prefix with struct" do
11341152
valid = %MySchema{id: 1}
11351153

0 commit comments

Comments
 (0)