@@ -2028,9 +2028,7 @@ defmodule Ecto.Schema do
20282028 writable = opts [ :writable ] || :always
20292029 put_struct_field ( mod , name , Keyword . get ( opts , :default ) )
20302030
2031- on_writable_violation = Keyword . get_lazy ( opts , :on_writable_violation , fn ->
2032- Module . get_attribute ( mod , :on_writable_violation , :nothing )
2033- end )
2031+ on_writable_violation = Keyword . get ( opts , :on_writable_violation )
20342032
20352033 redact_field? =
20362034 Keyword . get_lazy ( opts , :redact , fn ->
@@ -2086,7 +2084,9 @@ defmodule Ecto.Schema do
20862084 raise ArgumentError , "autogenerated fields must always be writable"
20872085 end
20882086
2089- Module . put_attribute ( mod , :ecto_on_writable_violation , { name , on_writable_violation } )
2087+ if on_writable_violation do
2088+ Module . put_attribute ( mod , :ecto_on_writable_violation , { name , on_writable_violation } )
2089+ end
20902090
20912091 if pk? do
20922092 Module . put_attribute ( mod , :ecto_primary_keys , name )
@@ -2387,6 +2387,7 @@ defmodule Ecto.Schema do
23872387 read_after_writes = Module . get_attribute ( module , :ecto_raw ) |> Enum . reverse ( )
23882388 autogenerate_id = Module . get_attribute ( module , :ecto_autogenerate_id )
23892389 on_writable_violation = Module . get_attribute ( module , :ecto_on_writable_violation )
2390+ on_writable_violation_default = Module . get_attribute ( module , :on_writable_violation , :nothing )
23902391
23912392 struct_fields = Module . get_attribute ( module , :ecto_struct_fields ) |> Enum . reverse ( )
23922393 derive = Module . get_attribute ( module , :derive )
@@ -2470,7 +2471,6 @@ defmodule Ecto.Schema do
24702471 { [ :embeds ] , embed_names } ,
24712472 { [ :updatable_fields ] , updatable } ,
24722473 { [ :insertable_fields ] , insertable } ,
2473- { [ :on_writable_violation ] , on_writable_violation |> Map . new ( ) |> Macro . escape ( ) } ,
24742474 { [ :redact_fields ] , redacted_fields } ,
24752475 { [ :autogenerate_fields ] , Enum . flat_map ( autogenerate , & elem ( & 1 , 0 ) ) } ,
24762476 { [ :virtual_fields ] , Enum . map ( virtual_fields , & elem ( & 1 , 0 ) ) } ,
@@ -2490,12 +2490,19 @@ defmodule Ecto.Schema do
24902490 { [ :type , quote ( do: _ ) ] , nil } ,
24912491 { [ :virtual_type , quote ( do: _ ) ] , nil } ,
24922492 { [ :association , quote ( do: _ ) ] , nil } ,
2493- { [ :embed , quote ( do: _ ) ] , nil }
2493+ { [ :embed , quote ( do: _ ) ] , nil } ,
2494+ { [ :on_writable_violation , quote ( do: _ ) ] , on_writable_violation_default }
24942495 ]
24952496
2497+ on_writable_violation_quoted =
2498+ for { name , value } <- on_writable_violation do
2499+ { [ :on_writable_violation , name ] , value }
2500+ end
2501+
24962502 bags_of_clauses =
24972503 [
24982504 single_arg ,
2505+ on_writable_violation_quoted ,
24992506 field_sources_quoted ,
25002507 types_quoted ,
25012508 virtual_types_quoted ,
0 commit comments