在 Phoenix 框架指南中显示 Ecto 模型字段的命令

Command to show fields of the Ecto model as in Phoenixframework guide

我想知道 Phoenixframework 指南网站的 Ecto 模型章节中显示实际用户的命令是什么 table:

hello_phoenix_dev=# \d users
Table "public.users"
Column     |            Type             |                     Modifiers
----------------+-----------------------------+----------------------------------------------------
id             | integer                     | not null default nextval('users_id_seq'::regclass)
name           | character varying(255)      |
email          | character varying(255)      |
bio            | character varying(255)      |
number_of_pets | integer                     |
inserted_at    | timestamp without time zone | not null
updated_at     | timestamp without time zone | not null
Indexes:
"users_pkey" PRIMARY KEY, btree (id)

列出的命令 (\d users) 应在您的 PostgreSQL 交互式终端 (psql) 提示符中调用:

psql -U postgres -W -h localhost hello_phoenix_dev

您在此处显示的用户 table 来自您的数据库。 Ecto 模型不知道你的数据库。您的 Ecto 模态表示将由您的 Repo 存储的数据(这是了解您的数据库的存储机制。)

您的 table 和架构匹配很重要。例如,对于您所展示的 table,您将具有以下架构:

  schema "users" do
    field :name, :string
    field :email, :string
    field :bio, :string
    field :number_of_pets, :integer

    timestamps
  end

这就是 Ecto 使用的方式,以便它知道当您搜索模型时 Repo 应该获取哪些数据。

如果您有以下架构:

  schema "users" do
    field :name, :string

    timestamps
  end

即使您的 table 中有 bioemailnumber_of_pets,它们也不会从数据库中获取或显示在您的 Elixir 结构中。