Instantly share code, notes, and snippets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Migration | |
defmodule Bookmarks.Repo.Migrations.CreateBookmark do | |
use Ecto.Migration | |
def change do | |
create table(:bookmarks) do | |
add :href, :string, null: false, size: 2000 | |
add :title, :string | |
add :description, :text | |
add :tags, {:array, :string} | |
timestamps | |
end | |
end | |
end | |
# models | |
defmodule Bookmarks.Bookmark do | |
use Bookmarks.Web, :model | |
schema "bookmarks" do | |
field :href, :string | |
field :title, :string | |
field :description, :string | |
belongs_to :user, User | |
embeds_many :tags, Bookmarks.Tag | |
timestamps | |
end | |
@required_fields ~w(href) | |
@optional_fields ~w(title description tags) | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, @required_fields, @optional_fields) | |
|> validate_length(:title, max: 255) | |
|> validate_length(:description, max: 2000) | |
|> validate_length(:href, min: 3) | |
|> validate_length(:href, max: 2000) | |
end | |
end | |
defmodule Bookmarks.Tag do | |
use Bookmarks.Web, :model | |
embedded_schema do | |
field :name, :string | |
end | |
end | |
# Failing test | |
test "add one tag to a bookmark" do | |
changeset = Bookmark.changeset(%Bookmark{}, %{href: "http://one.io", title: "title" }) | |
changeset = Ecto.Changeset.put_change(changeset, :tags, [%Tag{name: "one"}]) | |
bookmark = Repo.insert! changeset | |
assert bookmark.tags == ["one"] | |
end | |
## Repo.insert fails with the following error: | |
** (FunctionClauseError) no function clause matching in Postgrex.Extensions.Binary.encode/4 | |
stacktrace: | |
(postgrex) lib/postgrex/extensions/binary.ex:59: Postgrex.Extensions.Binary.encode(%Postgrex.TypeInfo{array_elem: 0, base_type: 0, comp_elems: [], input: "varcharin", oid: 1043, output: "varcharout", receive: "varcharrecv", send: "varcharsend", type: "varchar"}, %{id: "5e732b94-bdef-499e-91fc-07a7ec10a611", name: "one"}, 151599, {9, 4, 1}) | |
(postgrex) lib/postgrex/extensions/binary.ex:288: anonymous fn/3 in Postgrex.Extensions.Binary.encode_array/4 | |
(elixir) lib/enum.ex:1102: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3 | |
(postgrex) lib/postgrex/extensions/binary.ex:284: Postgrex.Extensions.Binary.encode_array/4 | |
(postgrex) lib/postgrex/extensions/binary.ex:256: Postgrex.Extensions.Binary.encode_array/3 | |
(postgrex) lib/postgrex/protocol.ex:299: anonymous fn/2 in Postgrex.Protocol.encode_params/1 | |
(elixir) lib/enum.ex:1043: anonymous fn/3 in Enum.map/2 | |
(elixir) lib/enum.ex:1387: Enum."-reduce/3-lists^foldl/2-0-"/3 | |
(elixir) lib/enum.ex:1043: Enum.map/2 | |
(postgrex) lib/postgrex/protocol.ex:294: Postgrex.Protocol.encode_params/1 | |
(postgrex) lib/postgrex/protocol.ex:264: Postgrex.Protocol.send_params/2 | |
(postgrex) lib/postgrex/protocol.ex:136: Postgrex.Protocol.message/3 | |
(postgrex) lib/postgrex/connection.ex:417: Postgrex.Connection.new_data/2 | |
(postgrex) lib/postgrex/connection.ex:292: Postgrex.Connection.handle_info/2 | |
(stdlib) gen_server.erl:615: :gen_server.try_dispatch/4 | |
(stdlib) gen_server.erl:681: :gen_server.handle_msg/5 | |
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment