2
votes

Was trying to build schemas for an existing set of tables using Ecto 2.1, in a Phoenix 1.3.0 app.

Example:

defmodule Book do
 use Ecto.Schema

 schema "books" do 
     field :title, :string
     field :owner_ids, {:array, :integer}
     field :borrower_ids, {:array, :integer}
     field :published, :boolean
 end
end

On the console when I do Book |> first |> Repo.one, I see the owner_ids are printed properly ["29"], but the borrower_ids shows '$'. Verified using psql that borrower_ids for that row in the table does have a list of values in the table, exactly like the owner_ids column. All other columns in the table print just fine. Anything I am missing here? Update: Rails/ActiveRecord 5.1.4 was able to retrieve this table and row just fine.

1

1 Answers

2
votes

'$' is a list containing the number 36:

iex> [36]
'$'

In a nutshell, every time Elixir sees a list of integers representing ASCII characters, it prints them between single quotes, because that's how Erlang strings are represented (also called charlists).

The i helper in IEx is very useful in those situations. When you see a value that you don't understand, you can use it to ask for more information:

iex(2)> i '$'
Term
  '$'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent valid
  ASCII characters. Conventionally, such lists of integers are referred to
  as "charlists" (more precisely, a charlist is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [36]
Reference modules
  List