Skip to content

Commit ed4323b

Browse files
committed
fix (Query#set): raise on foreign reference update attempt
1 parent 24110a5 commit ed4323b

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/onyx-sql/query/set.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ module Onyx::SQL
5656
raise "Cannot find an instance variable named @#{key} in #{T}" unless ivar
5757

5858
not_null = (a = ivar.annotation(Field) || ivar.annotation(Reference)) && a[:not_null]
59+
5960
raise "On Query(#{T})#set: #{key} is nilable in compilation time (`#{value}`), but #{T}@#{ivar.name} has `not_null` option set to `true`. Consider calling `.not_nil!` on the value" if not_null && value.nilable?
61+
62+
if (a = ivar.annotation(Reference)) && a[:foreign_key]
63+
raise <<-TEXT
64+
\e[41m Onyx::SQL compilation error \e[0m — Must not update \e[33m\e[1mUser@foo\e[0m in a query, because it's a foreign reference
65+
TEXT
66+
end
6067
%}
6168

6269
when {{key.symbolize}}

0 commit comments

Comments
 (0)