defmodule Post do
use Ecto.Schema
@primary_key false
embedded_schema do
field(:title, :string, default: "Untitled")
end
end
{:module, Post, <<70, 79, 82, 49, 0, 0, 12, ...>>,
[__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
defmodule User do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post)
embeds_one :settings, Settings, primary_key: false do
field(:dark_mode, :boolean, default: true)
end
end
end
{:module, User, <<70, 79, 82, 49, 0, 0, 14, ...>>,
[__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
%User{post: nil, settings: nil}
defmodule UserWithDefaults do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post, default: %Post{})
embeds_one :settings, Settings, primary_key: false, default: %Settings{} do
field(:dark_mode, :boolean, default: true)
end
end
end
{:module, UserWithDefaults, <<70, 79, 82, 49, 0, 0, 15, ...>>,
[__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
%UserWithDefaults{
post: %Post{title: "Untitled"},
settings: %UserWithDefaults.Settings{dark_mode: true}
}
defmodule UserWithCustomDefaults do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post, default: %Post{title: "Nice Title"})
embeds_one :settings, Settings, primary_key: false, default: %Settings{} do
field(:dark_mode, :boolean, default: true)
end
end
end
{:module, UserWithCustomDefaults, <<70, 79, 82, 49, 0, 0, 15, ...>>,
[__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
%UserWithCustomDefaults{}
%UserWithCustomDefaults{
post: %Post{title: "Nice Title"},
settings: %UserWithCustomDefaults.Settings{dark_mode: true}
}
try do
defmodule Error1 do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post, default: true)
embeds_one :settings, Settings, primary_key: false do
field(:dark_mode, :boolean, default: true)
end
end
end
rescue
e in ArgumentError ->
IO.puts(e.message)
end
invalid `:default` option for :post. The only valid option is a struct value like `%Post{}`
:ok
try do
defmodule Error2 do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post, default: %User{})
embeds_one :settings, Settings, primary_key: false do
field(:dark_mode, :boolean, default: true)
end
end
end
rescue
e in ArgumentError ->
IO.puts(e.message)
end
invalid `:default` option for :post. The only valid option is a struct value like `%Post{}`
:ok
try do
defmodule Error3 do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post)
embeds_one :settings, Settings, primary_key: false, default: true do
field(:dark_mode, :boolean, default: true)
end
end
end
rescue
e in ArgumentError ->
IO.puts(e.message)
end
invalid `:default` option for :settings. The only valid option is a struct value like `%Settings{}`
:ok
try do
defmodule Error4 do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post)
embeds_one :settings, Settings, primary_key: false, default: %User{} do
field(:dark_mode, :boolean, default: true)
end
end
end
rescue
e in ArgumentError ->
IO.puts(e.message)
end
invalid `:default` option for :settings. The only valid option is a struct value like `%Settings{}`
:ok
try do
defmodule Error5 do
use Ecto.Schema
@primary_key false
embedded_schema do
embeds_one(:post, Post)
embeds_one :settings, Settings, primary_key: false, default: %Settings{dark_mode: false} do
field(:dark_mode, :boolean, default: true)
end
end
end
rescue
e in ArgumentError ->
IO.puts(e.message)
end
invalid `:default` option for :settings. The only valid option is a struct value like `%Settings{}`
With inline embeds you also cannot override any defaults, so %Settings{dark_mode: false} is also invalid.
If you want to set defaults set it at the field definition or change a non-inline embed.
:ok