Show Menu
Cheatography

Ecto Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Mix tasks

ecto.c­reate
Creates the repository storage
ecto.c­reate -r Custom.Repo
ecto.drop
Drops the repository storage
ecto.drop -r Custom.Repo
ecto.dump
Dumps the repository database structure
ecto.dump -r Custom.Repo
ecto.dump -d /path/­to/file
ecto.g­en.m­ig­ration
Generates a new migration for the repo
ecto.g­en.m­ig­ration add_po­sts­_table
ecto.g­en.m­ig­ration add_po­sts­_table -r Custom.Repo
ecto.g­en.repo
Generates a new repository
ecto.g­en.repo -r Custom.Repo
ecto.load
Loads previously dumped database structure
ecto.load -r Custom.Repo
ecto.load -d /path/­to/file
ecto.m­igrate
Runs the repository migrations
ecto.m­igrate -r Custom.Repo
ecto.m­igrate -n(--step) 3
ecto.m­igrate -v(--to) 200809­061­20000
ecto.r­ollback
Rolls back the repository migrations
ecto.r­ollback -r Custom.Repo
ecto.r­ollback -n(--step) 3
ecto.r­ollback -v(--to) 200809­061­20000
ecto.r­ollback --all
 

Migrations

defmodule MyRepo.Migrations.MyMigration do
  use Ecto.Migration

  def up, do: ...
  def down, do: ...
  
  # or
  
  def change, do: ...
end
You can use either
up/0,  down/0
or
change/0

Migrat­ion­s.C­hange Table

create table(­:name) do
Create table macro
alter table(­:name) do
Alter table macro
create­_if­_no­t_exist table(­:name) do
Creates table, but not raise an error, if table already exist