Size: a a a

2019 November 19

SL

Sergey Loguntsov in pro.elixir
ок попробую
источник

SL

Sergey Loguntsov in pro.elixir
спасибо
источник

SL

Sergey Loguntsov in pro.elixir
def create(name, password) do
   hash = hash(password)
   user = %Chat.User{ :name => name, :password => hash }
   with { :ok, %Chat.User{} = user } <- Chat.Repo.insert(user) do
     { :ok, user }
   else
     %Ecto.ConstraintError{ :constraint : "name",  :type : :unique } ->
       { :error, "already_defined" }
   end
 end

он мне ругается на : в  %Ecto.ConstraintError{ :constraint : "name",  :type : :unique } ->

** (SyntaxError) lib/chat/user.ex:20: unexpected token: ":" (column 41, code point U+003A)
источник

SL

Sergey Loguntsov in pro.elixir
что я делаю не так ?
источник

RP

Roman Pushkov in pro.elixir
:constraint :
источник

RP

Roman Pushkov in pro.elixir
либо :constraint =>
источник

RP

Roman Pushkov in pro.elixir
либо constraint:
источник

SL

Sergey Loguntsov in pro.elixir
ога
источник

SL

Sergey Loguntsov in pro.elixir
спсб
источник

RP

Roman Pushkov in pro.elixir
вообще код странный конеш, with же исключения не ловит
источник

SL

Sergey Loguntsov in pro.elixir
вот именно . ) я только тоже хотел написать
источник

SL

Sergey Loguntsov in pro.elixir
try do
     Chat.Repo.insert(user)
   rescue
     %Ecto.ConstraintError{ constraint: "name",  type: :unique } ->
       { :error, "already_defined" }
   end

ругается что:
(CompileError) lib/chat/user.ex:20: invalid "rescue" clause. The clause should match on an alias, a variable or be in the "var in [alias]" format
источник

SL

Sergey Loguntsov in pro.elixir
чднт ?
источник

RP

Roman Pushkov in pro.elixir
если ты хочешь исключение отловить, то нужно что-то в духе
try do
 foo()
rescue
e in Ecto.ConstraintError ->
 bar()
end
источник

АН

Алексей Новоселов in pro.elixir
Roman Pushkov
вообще код странный конеш, with же исключения не ловит
так Chat.Repo.insert() тоже без ! не кидает исключения
источник

SL

Sergey Loguntsov in pro.elixir
так вот у меня кинул )
источник

RP

Roman Pushkov in pro.elixir
Алексей Новоселов
так Chat.Repo.insert() тоже без ! не кидает исключения
это был мой первый вопрос
источник

SL

Sergey Loguntsov in pro.elixir
iex(2)> Chat.User.create("test","test");
[debug] QUERY ERROR db=62.0ms queue=49.0ms
INSERT INTO user (name,password) VALUES (?,?) ["test", "CY9rzUYh03PK3k6DJie09g=="]
** (Ecto.ConstraintError) constraint error when attempting to insert struct:

   * name (unique_constraint)

If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call unique_constraint/3 on your changeset with the constraint
:name as an option.

The changeset has not defined any constraint.

   (ecto) lib/ecto/repo/schema.ex:689: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
   (elixir) lib/enum.ex:1336: Enum."-map/2-lists^map/1-0-"/2
   (ecto) lib/ecto/repo/schema.ex:674: Ecto.Repo.Schema.constraints_to_errors/3
   (ecto) lib/ecto/repo/schema.ex:274: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
   (chat) lib/chat/user.ex:17: Chat.User.create/2
источник

ŹR

Źmićer Rubinštejn in pro.elixir
Тебе же написано что надо сделать
источник

ŹR

Źmićer Rubinštejn in pro.elixir
Нужно в changeset добавить

|> unique_constraint(…)
источник