diff --git a/lib/ecto/schema.ex b/lib/ecto/schema.ex index 4ccff13cc3..de9d03d82c 100644 --- a/lib/ecto/schema.ex +++ b/lib/ecto/schema.ex @@ -1997,7 +1997,7 @@ defmodule Ecto.Schema do # better to raise unknown type first than unsupported option. type = check_field_type!(mod, name, type, opts) - if type == :any && !opts[:virtual] do + if type == :any && !opts[:virtual] && Module.get_attribute(mod, :ecto_source) do raise ArgumentError, "only virtual fields can have type :any, " <> "invalid type for field #{inspect(name)}" @@ -2301,6 +2301,7 @@ defmodule Ecto.Schema do end Module.put_attribute(module, :ecto_schema_defined, line) + Module.put_attribute(module, :ecto_source, source) if Code.can_await_module_compilation?() do Module.put_attribute(module, :after_verify, Ecto.Schema) diff --git a/test/ecto/schema_test.exs b/test/ecto/schema_test.exs index d84d78a78e..6c68168285 100644 --- a/test/ecto/schema_test.exs +++ b/test/ecto/schema_test.exs @@ -1185,6 +1185,15 @@ defmodule Ecto.SchemaTest do end end end + + # :any is allowed on embedded schema + defmodule EmbeddedFieldAny do + use Ecto.Schema + + embedded_schema do + field :json, :any + end + end end defmodule FieldAnyVirtual do