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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.