Skip to content

Commit 7767aab

Browse files
committed
feat: raise if reference primary key not defined on db mapping
1 parent 471b28c commit 7767aab

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/atom/model/db_mapping.cr

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ class Atom
7979
# ```
8080
{% pk = type["reference_type"].constant("MODEL_PRIMARY_KEY") %}
8181
{% pk_type = type["reference_type"].constant("MODEL_PRIMARY_KEY_TYPE") %}
82+
{%
83+
type = type
84+
unless pk && pk_type
85+
raise <<-TEXT
86+
Primary key of #{type["reference_type"]} is not defined at this moment of compilation.
87+
88+
Please try requiring it before using as a reference.
89+
90+
\e[32m+\e[39m️ Valid code:
91+
92+
\e[32mrequire "./#{type["reference_type"].stringify.underscore.id}"\e[39m # Actual path may be different
93+
94+
class #{@type}
95+
schema #{MODEL_TABLE} do
96+
type #{type["name"]} : #{type["type"]}
97+
end
98+
end
99+
TEXT
100+
end
101+
%}
82102

83103
{% if type["enumerable"] %}
84104
@{{type["name"]}} = rs.read({{type["enumerable"]}}({{pk_type}}) | Nil).try &.map do |pk|

0 commit comments

Comments
 (0)