O'Reilly logo

Programming Phoenix by Jose Valim, Bruce Tate, Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required