Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[debug] QUERY OK source="users" db=1.8ms decode=2.1ms queue=1.4ms idle=130.8ms
SELECT TRUE FROM "users" AS u0 WHERE (u0."email" = $1) LIMIT 1 ["kostas@live.com"]
#Ecto.Changeset<
action: nil,
changes: %{email: "kostas@live.com", password: "rubf6TFg"},
errors: [],
data: #Elijah.Accounts.User<>,
valid?: true
>
#Ecto.Changeset<
action: nil,
changes: %{email: "netsfr@gmail.com", password: "P5uakr6F"},
errors: [],
data: #Elijah.Accounts.User<>,
valid?: true
>
[debug] QUERY OK source="users" db=3.4ms idle=159.7ms
SELECT TRUE FROM "users" AS u0 WHERE (u0."email" = $1) LIMIT 1 ["netsfr@gmail.com"]
[debug] QUERY OK db=0.8ms idle=166.1ms
begin []
[debug] QUERY OK db=0.2ms
rollback []
** (Ecto.NoPrimaryKeyValueError) struct `#Elijah.Accounts.User<__meta__: #Ecto.Schema.Metadata<:built, "users">, avatar: nil, confirmed_at: nil, email: nil, hashed_password: nil, id: nil, inserted_at: nil, updated_at: nil, ...>` is missing primary key value
(ecto 3.4.6) lib/ecto/repo/schema.ex:868: anonymous fn/3 in Ecto.Repo.Schema.add_pk_filter!/2
(elixir 1.10.3) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto 3.4.6) lib/ecto/repo/schema.ex:310: Ecto.Repo.Schema.do_update/4
(ecto 3.4.6) lib/ecto/multi.ex:646: Ecto.Multi.apply_operation/5
(elixir 1.10.3) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto 3.4.6) lib/ecto/multi.ex:630: anonymous fn/5 in Ecto.Multi.apply_operations/5
(ecto_sql 3.4.5) lib/ecto/adapters/sql.ex:875: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
(db_connection 2.2.2) lib/db_connection.ex:1427: DBConnection.run_transaction/4
(ecto 3.4.6) lib/ecto/repo/transaction.ex:20: Ecto.Repo.Transaction.transaction/4
(elijah 0.1.0) lib/elijah/accounts.ex:119: Elijah.Accounts.register_users/1
(elixir 1.10.3) lib/code.ex:926: Code.require_file/2
(mix 1.10.3) lib/mix/tasks/run.ex:145: Mix.Tasks.Run.run/5
(mix 1.10.3) lib/mix/tasks/run.ex:85: Mix.Tasks.Run.run/1
(mix 1.10.3) lib/mix/task.ex:330: Mix.Task.run_task/3
(mix 1.10.3) lib/mix/cli.ex:82: Mix.CLI.run_task/2
def register_users(attrs_list) do
posts_multi =
attrs_list
|> Stream.map(fn user ->
%User{}
|> User.registration_changeset(user)
end)
|> Stream.map(fn user_cs ->
IO.inspect(user_cs)
# Create a Multi with a single update
# operation, generating a unique key for the op.
res = :crypto.hash(:sha256, user_cs.changes.email)
|> Base.encode16()
|> String.slice(-8..-2)
key = String.to_atom("post_#{res}")
Ecto.Multi.update(Ecto.Multi.new(), key, user_cs)
end)
|> Enum.reduce(Multi.new(), &Multi.append/2)
posts_multi
|> Repo.transaction()
|> case do
{:ok, %{user: user}} ->
{:ok, user}
{:error, _, error, _} ->
{:error, error}
end
end
@mazz
Copy link
Author

mazz commented Sep 16, 2020

sql ddl:

-- DDL generated by Postico 1.5.12
-- Not all database features are supported. Do not use for backup.

-- Table Definition ----------------------------------------------

CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    email citext NOT NULL,
    hashed_password character varying(255) NOT NULL,
    confirmed_at timestamp(0) without time zone,
    inserted_at timestamp(0) without time zone NOT NULL,
    updated_at timestamp(0) without time zone NOT NULL,
    avatar character varying(255)
);

-- Indices -------------------------------------------------------

CREATE UNIQUE INDEX users_pkey ON users(id int8_ops);
CREATE UNIQUE INDEX users_email_index ON users(email citext_ops);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment