【问题标题】:Phoenix/Ecto - integer values in a Postgres array column not being retrievedPhoenix/Ecto - 未检索 Postgres 数组列中的整数值
【发布时间】:2018-01-15 19:30:36
【问题描述】:

正在尝试在 Phoenix 1.3.0 应用中使用 Ecto 2.1 为现有的一组表构建架构。

例子:

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

当我在控制台上执行Book |> first |> Repo.one 时,我看到owner_ids 打印正确["29"],但borrower_ids 显示'$'。使用psql 验证,表中该行的borrower_ids 确实具有表中的值列表,与owner_ids 列完全相同。 表中的所有其他列都打印得很好。我在这里缺少什么吗? 更新:Rails/ActiveRecord 5.1.4 能够很好地检索此表和行。

【问题讨论】:

    标签: phoenix-framework ecto


    【解决方案1】:

    '$' 是一个包含数字 36 的列表:

    iex> [36]
    '$'
    

    简而言之,每次 Elixir 看到表示 ASCII 字符的整数列表时,它都会在单引号之间打印它们,因为这就是 Erlang 字符串的表示方式(也称为 charlists)。

    IEx 中的i 助手在这些情况下非常有用。当您看到一个您不理解的值时,您可以使用它来询问更多信息:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 2017-12-06
      • 2012-01-04
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多