Ecto CastError: value in `join` cannot be cast to type {:in, :string}

56 views Asked by At

I had a list defined like so:

flags = ['flag_a', 'flag_b']

And a query set up something like this:

from(u in User,
     left_join: f in Flags,
     on: f.account_id == u.account_id and f.name in ^@flags

Which gave me this error message:

(Ecto.Query.CastError) lib/service/users.ex:106: value `['flag_a', 'flag_b']` in `join` cannot be cast to type {:in, :string} in query:

... etc ...
2

There are 2 answers

0
Hannele On BEST ANSWER

I just needed to use double-quotes for the values:

flags = ["flag_a", "flag_b"]

So that the values in the list were strings and not char lists.

1
Onorio Catenacci On

FWIW, this would also work:

flags = ~w(flag_a flag_b)   #["flag_a", "flag_b"]

or

flags = ~w(flag_a flag_b)s  #["flag_a", "flag_b"]

and this would be equivalent to your original code:

flags = ~w(flag_a flag_b)c  #[~c"flag_a", ~c"flag_b"]

I believe I saw somewhere that the use of single quotes to delimit charlists is deprecated and one should use the ~c sigil but I can't seem to find my reference.

Finally, not to split semantic hairs--what you had at first (flags = ['flag_a', 'flag_b']) was a list of char lists. flags=["flag_a", "flag_b"] isn't a string; it's a list of strings.