Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass allow_stale opt to assocs #4528

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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