Defining the User Schema and Migration

Ecto has a DSL that specifies the fields in a struct and the mapping between those fields and the database tables. Let’s use that now. To define our schema, replace the contents in web/models/user.ex with the following:

 defmodule​ Rumbl.User ​do
 use​ Rumbl.Web, ​:model
 
  schema ​"​​users"​ ​do
  field ​:name​, ​:string
  field ​:username​, ​:string
  field ​:password​, ​:string​, ​virtual:​ true
  field ​:password_hash​, ​:string
 
  timestamps
 end
 end

This DSL is built with Elixir macros. The schema and field macros let us specify both the underlying database table and the Elixir struct. Each field corresponds to both a field in the database and ...

Get Programming Phoenix now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.