View Source CRUD Cheatsheet
Assume that your schema looks like the following:
defmodule MyApp.Person do
import Ecto.Changeset
use Ecto.Schema
use Ecto.Entity # Gems brought by the Ecto Entity
schema "people" do
field :first_name, :string
field :last_name, :string
field :age, :integer
end
def changeset(entity, attrs) do
entity
|> cast(attrs, [:first_name, :last_name])
|> validate_required([:first_name, :last_name])
end
end
create
CREATE
create-insert-new-records
Create / Insert new records
iex(1)> MyApp.Person.create(%{
... first_name: "Hand",
... last_name: "Turner",
... age: 3
})
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 5,
first_name: "Hand",
last_name: "Turner",
age: 3
}}
create-many
Create Many
iex(1)> MyApp.Person.create_many([
... %{first_name: "Parisian", last_name: "Beier", age: 7},
... %{first_name: "Lang", last_name: "Emard", age: 3}
])
{:ok, %{insert_all: {2, nil}}}
read
READ
retrieve-all-entries
Retrieve all entries
iex(1)> MyApp.Person.all()
[
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "German",
last_name: "OConnell",
age: 2
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 2,
first_name: "Fritsch",
last_name: "Kassulke",
age: 8
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 3,
first_name: "Russel",
last_name: "Collins",
age: 3
}
]
finding-an-entry-by-id
Finding an entry by ID
iex(1)> MyApp.Person.find(5)
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 5,
first_name: "Kristopher",
last_name: "Keeling",
age: 9
}
retrieve-many-by-identifiers
Retrieve many by identifiers
iex(1)> Person.find([1, 2, 3])
[
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "Bruen",
last_name: "Sporer",
age: 2
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 2,
first_name: "Herman",
last_name: "Jerde",
age: 2
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 3,
first_name: "Macey",
last_name: "Treutel",
age: 9
}
]
retrieve-a-number-of-entries
Retrieve a number of entries
iex(1)> Person.take(2)
[
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "German",
last_name: "OConnell",
age: 2
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 2,
first_name: "Fritsch",
last_name: "Kassulke",
age: 8
}
]
update
UPDATE
update-a-record-by-identified-by-id
Update a record by identified by ID
iex(1)> Person.update(1, %{first_name: "Kamaro"})
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "Kamaro",
last_name: "Yundt",
age: 7
}}
update-many-records-at-once
Update Many Records at once
iex(1)> query = Person.not_in_ids(1)
#Ecto.Query<from p0 in Person, where: p0.id not in ^[1]>
iex(2)> Person.update_many(query, [first_name: "Kamaro Paul"])
{:ok, %{update_all: {2, nil}}}
update-by-entity
Update by entity
iex(1)> person = Person.find(1)
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "Weber",
last_name: "Ok 2",
age: 7
}
iex(2)> Person.update(person, %{first_name: "Kamaro"})
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "Kamaro",
last_name: "Ok 2",
age: 7
}}
delete
DELETE
delete-an-entry
Delete an entry
iex(1)> Person.delete(7)
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:deleted, "people">,
id: 7,
first_name: "Glover",
last_name: "Schimmel",
age: 2
}}
delete-entries-except-record-matching-ids
Delete entries except record matching IDs
iex(1)> Person.delete_except([47, 48])
{48, nil}
empty-the-entire-table
Empty the entire table
iex(1)> Person.truncate()
{:ok,
%MyXQL.Result{
columns: nil,
connection_id: 788,
last_insert_id: 0,
num_rows: 0,
rows: nil,
num_warnings: 0
}}