Skip to content

Instantly share code, notes, and snippets.

@mazz
Last active December 3, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazz/083a7c1710649802bf7a9f43a70cb284 to your computer and use it in GitHub Desktop.
Save mazz/083a7c1710649802bf7a9f43a70cb284 to your computer and use it in GitHub Desktop.
defmodule Pearls.Repo.Migrations.CreateBibleBooks do
use Ecto.Migration
def change do
create table(:bible_books) do
add :name, :string
add :bible_id, references(:bible_versions, on_delete: :nothing)
add :testament, :string, null: false
timestamps()
end
create index(:bible_books, [:bible_id])
end
end
defmodule Pearls.Schema.BibleBook do
use Ecto.Schema
import Ecto.Changeset
schema "bible_books" do
field :name, :string
field :bible_id, :id
field :testament, Ecto.Enum, values: [:new, :old]
timestamps()
end
@doc false
def changeset(book, attrs) do
IO.inspect(book, label: "changeset book")
IO.inspect(attrs, label: "changeset attrs")
book
|> cast(attrs, [:name, :bible_id, :testament])
|> validate_required([:name, :bible_id, :testament])
end
end
changeset book: %Pearls.Schema.BibleBook{
__meta__: #Ecto.Schema.Metadata<:built, "bible_books">,
bible_id: nil,
id: nil,
inserted_at: nil,
name: nil,
testament: nil,
updated_at: nil
}
changeset attrs: %{bible_id: 1, name: "Numbers", testament: :old}
book_change: #Ecto.Changeset<
action: nil,
changes: %{bible_id: 1, name: "Numbers", testament: :old},
errors: [],
data: #Pearls.Schema.BibleBook<>,
valid?: true
>
[debug] QUERY OK db=2.8ms queue=1.4ms idle=322.2ms
INSERT INTO "bible_books" ("bible_id","name","testament","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" [1, "Numbers", "old", ~N[2020-12-03 17:36:59], ~N[2020-12-03 17:36:59]]
** (Ecto.InvalidChangesetError) could not perform insert because changeset is invalid.
Errors
%{testament: [{"can't be blank", [validation: :required]}]}
Applied changes
%{name: 1}
Params
%{"name" => 1}
Changeset
#Ecto.Changeset<
action: :insert,
changes: %{name: 1},
errors: [testament: {"can't be blank", [validation: :required]}],
data: #Pearls.Schema.BibleChapter<>,
valid?: false
>
(ecto 3.5.5) lib/ecto/repo/schema.ex:169: Ecto.Repo.Schema.insert!/4
priv/repo/seeds.exs:69: anonymous fn/2 in :elixir_compiler_1.__FILE__/1
(elixir 1.11.1) lib/enum.ex:1399: Enum."-map/2-lists^map/1-0-"/2
(elixir 1.11.1) lib/code.ex:931: Code.require_file/2
(mix 1.11.1) lib/mix/tasks/run.ex:146: Mix.Tasks.Run.run/5
(mix 1.11.1) lib/mix/tasks/run.ex:86: Mix.Tasks.Run.run/1
(mix 1.11.1) lib/mix/task.ex:394: Mix.Task.run_task/3
(mix 1.11.1) lib/mix/task.ex:443: Mix.Task.run_alias/5
(mix 1.11.1) lib/mix/cli.ex:84: Mix.CLI.run_task/2
(elixir 1.11.1) lib/code.ex:931: Code.require_file/2
book_change = BibleBook.changeset(%BibleBook{}, %{bible_id: stored_bible.id, name: book_name, testament: testament})
|> Ecto.Changeset.put_change(:testament, testament)
|> Ecto.Changeset.put_change(:bible_id, stored_bible.id)
IO.inspect(book_change, label: "book_change")
book_stored = Repo.insert!(book_change)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment