Skip to content

Ecto.Schema: Allow embedded_schema to have non-virtual :any fields#4729

Merged
wojtekmach merged 2 commits into
masterfrom
wm-embed-virtual
May 22, 2026
Merged

Ecto.Schema: Allow embedded_schema to have non-virtual :any fields#4729
wojtekmach merged 2 commits into
masterfrom
wm-embed-virtual

Conversation

@wojtekmach
Copy link
Copy Markdown
Member

No description provided.

Comment thread lib/ecto/schema.ex Outdated
Comment on lines +2000 to +2003
fields = Module.get_attribute(mod, :ecto_struct_fields)
schema_with_source? = match?(%Ecto.Schema.Metadata{}, fields[:__meta__])

if schema_with_source? && type == :any && !opts[:virtual] do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we store @ecto_source and then we check if it is nil? Loading all fields from attributes can get expensive.

We should also move the expensive check last:

Suggested change
fields = Module.get_attribute(mod, :ecto_struct_fields)
schema_with_source? = match?(%Ecto.Schema.Metadata{}, fields[:__meta__])
if schema_with_source? && type == :any && !opts[:virtual] do
if type == :any && !opts[:virtual] &&
match?(%Ecto.Schema.Metadata{}, Module.get_attribute(mod, :ecto_struct_fields)[:__meta__]) do

@wojtekmach wojtekmach merged commit af35386 into master May 22, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants