Skip to content

Commit

Permalink
Pass allow_stale opt to assocs (#4528)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomconroy authored Oct 20, 2024
1 parent 0d2a56e commit 4f0c990
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/ecto/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1660,9 +1660,9 @@ defmodule Ecto.Repo do
* `:stale_error_message` - The message to add to the configured
`:stale_error_field` when stale errors happen, defaults to "is stale".
* `:allow_stale` - Doesn't error if insert is stale. Defaults to `false`.
* `:allow_stale` - Doesn't error when structs are stale. Defaults to `false`.
This may happen if there are rules or triggers in the database that
rejects the insert operation.
rejects the insert operation. This option cascades to associations.
See the ["Shared options"](#module-shared-options) section at the module
documentation for more options.
Expand Down Expand Up @@ -1863,7 +1863,7 @@ defmodule Ecto.Repo do
* `:allow_stale` - Doesn't error if update is stale. Defaults to `false`.
This may happen if the struct has been deleted from the database before
the update or if there is a rule or a trigger on the database that rejects
the update operation.
the update operation. This option cascades to associations.
See the ["Shared options"](#module-shared-options) section at the module
documentation for more options.
Expand Down Expand Up @@ -1909,8 +1909,8 @@ defmodule Ecto.Repo do
* `:stale_error_message` - The message to add to the configured
`:stale_error_field` when stale errors happen, defaults to "is stale".
Only applies to updates.
* `:allow_stale` - Doesn't error if delete is stale. Defaults to `false`.
Only applies to updates.
* `:allow_stale` - Doesn't error when structs are stale. Defaults to `false`.
This option cascades to associations.
See the ["Shared options"](#module-shared-options) section at the module
documentation for more options.
Expand Down
2 changes: 1 addition & 1 deletion lib/ecto/repo/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ defmodule Ecto.Repo.Schema do
defp assoc_opts([], _opts), do: []

defp assoc_opts(_assocs, opts) do
Keyword.take(opts, [:timeout, :log, :telemetry_event, :prefix])
Keyword.take(opts, [:timeout, :log, :telemetry_event, :prefix, :allow_stale])
end

defp process_parents(changeset, user_changeset, assocs, reset_assocs, adapter, opts) do
Expand Down
20 changes: 19 additions & 1 deletion test/ecto/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ defmodule Ecto.RepoTest do

schema "my_schema_child" do
field :a, :string
belongs_to :my_schema, MySchemaNoPK, references: :n, foreign_key: :n
belongs_to :my_schema, MySchema
belongs_to :my_schema_no_pk, MySchemaNoPK, references: :n, foreign_key: :n
end

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

test "insert, update allows stale children with :allow_stale option" do
child_schema =
%MySchemaChild{a: "one"}

stale =
put_in(child_schema.__meta__.context, {:error, :stale})
|> Ecto.Changeset.change()

changeset =
%MySchema{id: 1}
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:children, [stale])

assert {:ok, _} = TestRepo.insert(changeset, allow_stale: true)
assert {:ok, _} = TestRepo.update(changeset, allow_stale: true)
end

test "insert and delete sets schema prefix with struct" do
valid = %MySchema{id: 1}

Expand Down

0 comments on commit 4f0c990

Please sign in to comment.