Insert/Update

Insert

Repo.insert(%User{email: "user1@example.com"}) 
#{:ok, [new User object...]}

Update

Unlike insert, get, or delete, update NEEDS a changeset

user
|> User.changeset(attrs)
|> Repo.update()
# {:ok, %User{} = user}
    
MyRepo.update_all(Post, set: [title: "New title"])

MyRepo.update_all(Post, inc: [visits: 1])

from(p in Post, where: p.id < 10, select: p.visits)
|> MyRepo.update_all(set: [title: "New title"])

Advanced

Insert or Update, Upsert

Works most of the time:

Race condition immune

Method 1:

*note a unique index must be on daily_recording_id

Method 2, reget on changeset error:

Composing queries

Fragments

Allow you to define raw SQL

Custom Macros

Last updated