Constraint error when attempting to insert struct - foreign_key_constraint/3

I have an error in my application when registering users …
Has anyone ever experienced this?

Error:

[info] POST /users
[debug] Processing with GcallWeb.UserController.create/2
Parameters: %{"_csrf_token" => “AxtQXAszfyEcGRBlJzwNSzlJGn4BIShQdZgh3eOcOAQTAuefv-rOxtFi”, “_utf8” => “✓”, “user” => %{“email” => "teste1@teste.com", “password” => “[FILTERED]”}}
Pipelines: [:browser]
[info] Replied phoenix:live_reload :ok
[debug] QUERY ERROR db=10.3ms queue=0.2ms
INSERT INTO users (email,password) VALUES (?,?) [“teste1@teste.com”, “$2b$12$nq/MpbfmnX8xG3coc5h7z.sgBJfXSVXLSShL9ytyPrYsoI0TAsjXC”]
[info] Sent 500 in 278ms
[error] #PID<0.680.0> running GcallWeb.Endpoint (connection #PID<0.678.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: POST /users
** (exit) an exception was raised:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:

* fk_user_profile (foreign_key_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 foreign_key_constraint/3 on your changeset with the constraint
:name as an option.

The changeset defined the following constraints:

* users_email_index (unique_constraint)

    (ecto) lib/ecto/repo/schema.ex:687: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (ecto) lib/ecto/repo/schema.ex:672: Ecto.Repo.Schema.constraints_to_errors/3
    (ecto) lib/ecto/repo/schema.ex:274: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
    (gcall) lib/gcall_web/controllers/user_controller.ex:25: GcallWeb.UserController.create/2
    (gcall) lib/gcall_web/controllers/user_controller.ex:1: GcallWeb.UserController.action/2
    (gcall) lib/gcall_web/controllers/user_controller.ex:1: GcallWeb.UserController.phoenix_controller_pipeline/2
    (gcall) lib/gcall_web/endpoint.ex:1: GcallWeb.Endpoint.instrument/4
    (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
    (gcall) lib/gcall_web/endpoint.ex:1: GcallWeb.Endpoint.plug_builder_call/2
    (gcall) lib/plug/debugger.ex:122: GcallWeb.Endpoint."call (overridable 3)"/2
    (gcall) lib/gcall_web/endpoint.ex:1: GcallWeb.Endpoint.call/2
    (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2
    (cowboy) /home/gilberto/Projetos/elixir/gcall/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
    (cowboy) /home/gilberto/Projetos/elixir/gcall/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
    (cowboy) /home/gilberto/Projetos/elixir/gcall/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

[info] Replied phoenix:live_reload :ok

Note: I registered some users manually in the database

Controller:

  def create(conn, %{"user" => user_params}) do
    case Structure.create_user(user_params) do
      {:ok, user} ->
        conn
        |> put_session(:current_user_id, user.id)
        |> put_flash(:info, "User created successfully.")
        |> redirect(to: Routes.user_path(conn, :index))

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

Schema:

  alias Comeonin.Bcrypt

  schema "users" do
    field :dashboard, :string
    field :email, :string
    field :name, :string
    field :password, :string
    field :profile_id, :integer
#    field :validate, :boolean, default: false

#    timestamps()
  end

  @doc false
  def changeset(user, attrs) do
    user
    |> cast(attrs, [:name, :password, :email, :dashboard, :profile_id])
    |> validate_required([:password, :email])
    |> unique_constraint(:email)
    |> update_change(:password, &Bcrypt.hashpwsalt/1)
  end

:wave:

Seems like the associated profile doesn’t exist.

4 Likes

Sorry for the question, but it was required fields in the database.
Thank you :man_facepalming:

1 Like

You’re a saint, I just spent 30 minutes on this and it was that easy… :smiley:

1 Like