Skip to content

Conversation

skloibi
Copy link
Contributor

@skloibi skloibi commented Jun 18, 2020

This PR fixes a potential compilation issue that appears in certain edge cases where the explicit typecast during SQL generation somehow confuses the type inference.

This does not seem to be a frequent issue as it only ever happened to me in one occasion where I had two models without date / timestamp columns (the following are abstractions of those)

class Sample < Jennifer::Model::Base
  table_name "samples"

  mapping({
    id:             Int64,
    name:       String?,
    secret:      String?
  })
  

  has_and_belongs_to_many :others, Other, join_table: "sample_others", association_foreign: "other_id", foreign: "sample_id"
end
class Other < Jennifer::Model::Base
  table_name "others"

  mapping({
    id:             Int64,
    title:       String?
  })
end

which are connected via a many-to-many relation (with one-sided access).

When calling sample.remove_others(other), this leads to a corresponding compilation error as the type inference algorithm seems to believe that the cast in

def self.parse_query(query, args : Array(DBAny))
arr = Array(String).new(args.size)
args.each_with_index do |arg, i|
args[i] = arg.as(Time).to_utc if arg.is_a?(Time)
arr << "$#{i + 1}"
end
{query % arr, args}
end

more specifically
args[i] = arg.as(Time).to_utc if arg.is_a?(Time)

is invalid.

The error is then as follows:

In lib/jennifer/src/jennifer/adapter/postgres/sql_generator.cr:126:15

 126 | args[i] = arg.as(Time).to_utc if arg.is_a?(Time)
           ^
Error: no overload matches 'Array(Bool | Float64 | Int64 | String | Nil)#[]=' with types Int32, Time

Overloads are:
 - Array(T)#[]=(index : Int, count : Int, values : Array(T))
 - Array(T)#[]=(index : Int, count : Int, value : T)
 - Array(T)#[]=(index : Int, value : T)
 - Array(T)#[]=(range : Range, values : Array(T))
 - Array(T)#[]=(range : Range, value : T)

Appendix

Since this seems to be also a bug in the compiler / type inference algorithm, it would probably worth it to also post it there. I wanted to push this here first, as it is easy to fix for this simple case - please let me know what you think about that.

@imdrasil imdrasil self-requested a review June 22, 2020 06:44
Copy link
Owner

@imdrasil imdrasil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes Crystal type resolving behaves inconsistent - hope in 1.0.0 it will be more stable. 👍

@imdrasil imdrasil merged commit 4a9d552 into imdrasil:master Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants