Skip to Main Content
Programming Phoenix
book

Programming Phoenix

by Chris McCord, Bruce Tate, Jose Valim
April 2016
Beginner to intermediate content levelBeginner to intermediate
300 pages
6h 58m
English
Pragmatic Bookshelf
Content preview from Programming Phoenix

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Elm

Programming Elm

Jeremy Fairbank
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781680501926Errata Page