Skip to content

Eager loading causes DataTypeCasting exception on sqlite #403

@wonderix

Description

@wonderix

Calling eager_load with sqlite always produces a DataTypeCasting Column <Model>.id can't be casted from Int64 to it's type - (Int32 | Nil).

I'm using a simple model with User, Team and Member and the following code is causing the exception

Team.all.eager_load(members: [:user])

This can be fixed by adding the following code in mapping.cr at line 438

--- a/src/jennifer/model/mapping.cr
+++ b/src/jennifer/model/mapping.cr
@@ -438,6 +438,8 @@ module Jennifer
               %casted_var{key.id} =
                 {% if value[:parsed_type] =~ /String/ %}
                   %var{key.id}
+                {% elsif value[:parsed_type] =~ /Int32/ %}
+                  (%var{key.id}.is_a?(Int64) ? %var{key.id}.to_i32 : %var{key.id})
                 {% else %}
                   (%var{key.id}.is_a?(String) ? self.class.coerce_{{key.id}}(%var{key.id}) : %var{key.id})
                 {% end %}

I'm not sure if this would be the right place for this kind of fix.

I'm using

  • crystal: 1.3.2
  • jennifer.cr: master
  • jennifer_sqlite3_adapter: 0.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions