Paulo Janeiro
Paulo Janeiro

Reputation: 3221

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

I would like to know what's the command that shows the actual users table that appears in the Ecto Model chapter in the Phoenixframework guides website:

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)

Upvotes: 0

Views: 124

Answers (1)

Gazler
Gazler

Reputation: 84180

The command listed (\d users) should be called inside your PostgreSQL interactive terminal (psql) prompt:

psql -U postgres -W -h localhost hello_phoenix_dev

The users table that you have displayed there comes from your database. Ecto models are not aware of your database. Your Ecto modal is a representation of the data that will be stored by your Repo (this is the storage mechanism that does know about your database.)

It is important that your table and schema match. For example, for the table you have shown you would have the following schema:

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

    timestamps
  end

This is what Ecto uses so that it knows what data should be fetched by the Repo when you search for a model.

If you had the following schema:

  schema "users" do
    field :name, :string

    timestamps
  end

Even though you have bio, email and number_of_pets in your table, they won't be fetched from the database or displayed in your Elixir struct.

Upvotes: 1

Related Questions