Skip to content

Instantly share code, notes, and snippets.

@alex-min
Created February 28, 2019 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-min/f29091438c15d76405d636355cf2b4e9 to your computer and use it in GitHub Desktop.
Save alex-min/f29091438c15d76405d636355cf2b4e9 to your computer and use it in GitHub Desktop.
crecto error
Error in crecto_test.rb:28: instantiating 'Repo:Module#all(Post.class)'
Repo.all Post
^~~
in lib/crecto/src/crecto/repo.cr:76: instantiating 'add_preloads(Array(Post), Post.class, Array(NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil)))'
add_preloads(results, queryable, preloads)
^~~~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:636: instantiating 'Array(NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))#each()'
preloads.each do |preload|
^~~~
in /usr/share/crystal/src/indexable.cr:187: instantiating 'each_index()'
each_index do |i|
^~~~~~~~~~
in /usr/share/crystal/src/indexable.cr:187: instantiating 'each_index()'
each_index do |i|
^~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:636: instantiating 'Array(NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))#each()'
preloads.each do |preload|
^~~~
in lib/crecto/src/crecto/repo.cr:639: instantiating 'has_many_preload(Array(Post), Post.class, NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))'
has_many_preload(results, queryable, preload)
^~~~~~~~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:654: instantiating 'join_through(Array(Post), Post.class, NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))'
join_through(results, queryable, preload)
^~~~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:689: instantiating 'all(Crecto::Model+.class, Crecto::Repo::Query)'
join_table_items = all(association_klass, join_query)
^~~
in lib/crecto/src/crecto/repo.cr:76: instantiating 'add_preloads((Array(Post) | Array(User)), Crecto::Model+.class, Array(NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil)))'
add_preloads(results, queryable, preloads)
^~~~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:636: instantiating 'Array(NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))#each()'
preloads.each do |preload|
^~~~
in /usr/share/crystal/src/indexable.cr:187: instantiating 'each_index()'
each_index do |i|
^~~~~~~~~~
in /usr/share/crystal/src/indexable.cr:187: instantiating 'each_index()'
each_index do |i|
^~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:636: instantiating 'Array(NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))#each()'
preloads.each do |preload|
^~~~
in lib/crecto/src/crecto/repo.cr:639: instantiating 'has_many_preload((Array(Post) | Array(User)), Crecto::Model+.class, NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))'
has_many_preload(results, queryable, preload)
^~~~~~~~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:654: instantiating 'join_through((Array(Post) | Array(User)), Crecto::Model+.class,NamedTuple(symbol: Symbol, query: Crecto::Repo::Query | Nil))'
join_through(results, queryable, preload)
^~~~~~~~~~~~
in lib/crecto/src/crecto/repo.cr:702: instantiating 'Crecto::Model+.class#foreign_key_value_for_association(Symbol, (Array(Bool | Float32 | Float64 | Int16 | Int32 | Int64 | Int8 | JSON::Any | String | Time | Nil) | Array(Int16 | Int32 | Int64 | Int8 | String | Nil) | Array(Int32) | Array(Int64) | Array(String) | Bool | Float32 | Float64 | Int16 | Int32 | Int64 | Int8 | JSON::Any | String | Time | Nil))'
join_ids = join_table_items.map { |i| association_klass.foreign_key_value_for_association(queryable.through_key_for_association(preload[:symbol]).as(Symbol), i) }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
in macro 'belongs_to' /home/alex/work/amber/abcdef/lib/crecto/src/crecto/schema/belongs_to.cr:7, line 2:
1.
> 2. Crecto::Schema::Associations.setup_associations
3.
4.
5. @user : User?
6.
7. def user? : User?
8. @user
9. end
10.
11. def user : User
12. user? || raise Crecto::AssociationNotLoaded.new("Association `user' is not loaded or is nil. Use `user?' if the association is nilable.")
13. end
14.
15.
16.
17.
18.
19. field :user_id, PkeyValue
20.
21.
22. def user=(val : User?)
23. @user = val
24. return if val.nil?
25. @user_id = val.pkey_value.as(PkeyValue)
26. end
27.
28. CRECTO_ASSOCIATIONS.push({
29. association_type: :belongs_to,
30. key: :user,
31. this_klass: Post,
32. klass: User,
33. foreign_key: :user_id,
34. foreign_key_value: ->(item : Crecto::Model){
35. item.as(Post).user_id.as(PkeyValue)
36. },
37. set_association: ->(self_item : Crecto::Model, items : Array(Crecto::Model) | Crecto::Model){
38. self_item.as(Post).user = items.as(Array(Crecto::Model))[0].as(User);nil
39. },
40. through: nil
41. })
42.
expanding macro
in macro 'setup_associations' /home/alex/work/amber/abcdef/lib/crecto/src/crecto/schema/associations.cr:4, line 30:
1. # :nodoc:
2. CRECTO_ASSOCIATIONS = Array(NamedTuple(association_type: Symbol,
3. key: Symbol,
4. this_klass: Crecto::Model.class,
5. klass: Crecto::Model.class,
6. foreign_key: Symbol,
7. foreign_key_value: Proc(Crecto::Model, PkeyValue),
8. set_association: Proc(Crecto::Model, (Array(Crecto::Model) | Crecto::Model), Nil),
9. through: Symbol?)).new
10.
11. # Get the Class for the assocation name
12. # i.e. :posts => Post
13. def self.klass_for_association(association : Symbol)
14. CRECTO_ASSOCIATIONS.select{|a| a[:key] == association && a[:this_klass] == self}.first[:klass]
15. end
16.
17. # Get the foreign key for the association
18. # i.e. :posts => :user_id
19. def self.foreign_key_for_association(association : Symbol) : Symbol?
20. CRECTO_ASSOCIATIONS.select{|a| a[:key] == association && a[:this_klass] == self}.first[:foreign_key]
21. end
22.
23. def self.foreign_key_for_association(klass : Crecto::Model.class)
24. CRECTO_ASSOCIATIONS.select{|a| a[:klass] == klass && a[:this_klass] == self}.first[:foreign_key]
25. end
26.
27. # Get the foreign key value from the relation object
28. # i.e. :posts, post => post.user_id
29. def self.foreign_key_value_for_association(association : Symbol, item)
> 30. CRECTO_ASSOCIATIONS.select{|a| a[:key] == association && a[:this_klass] == self}.first[:foreign_key_value].call(item).as(PkeyValue)
31. end
32.
33. # Set the value for the association
34. # i.e. :posts, user, [posts] => user.posts = [posts]
35. def self.set_value_for_association(association : Symbol, item, items)
36. CRECTO_ASSOCIATIONS.select{|a| a[:key] == association && a[:this_klass] == self}.first[:set_association].call(item, items)
37. end
38.
39. # Get the association type for the association
40. # i.e. :posts => :has_many
41. def self.association_type_for_association(association : Symbol)
42. CRECTO_ASSOCIATIONS.select{|a| a[:key] == association && a[:this_klass] == self}.first[:association_type]
43. end
44.
45. # Get the through association symbol
46. # i.e. :posts => :user_posts (if has_many through)
47. def self.through_key_for_association(association : Symbol) : Symbol?
48. CRECTO_ASSOCIATIONS.select{|a| a[:key] == association && a[:this_klass] == self}.first[:through]
49. end
50.
no overload matches 'Proc(Crecto::Model, (Int16 | Int32 | Int64 | Int8 | String | Nil))#call' with type (Array(Bool | Float32 | Float64 | Int16 | Int32 | Int64 | Int8 | JSON::Any | String | Time | Nil) | Array(Int16 | Int32 | Int64 |Int8 | String | Nil) | Array(Int32) | Array(Int64) | Array(String) | Bool | Float32 | Float64 | Int16 | Int32 | Int64 | Int8 | JSON::Any | String | Time | Nil)
Overloads are:
- Proc(T, R)#call(*args : *T)
Rerun with --error-trace to show a complete error trace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment