Owens

Owens

(Ecto.ConstraintError) constraint error when attempting to insert struct: (foreign_key_constraint)

Hello all,

Strangely, I’m getting this error in production (Gigalixir) but not on local.

ERROR

Request: POST /parent_organizations/1/users

** (exit) an exception was raised:
    ** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * parent_organizations_users_parent_organization_id_fkey (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 has not defined any constraint.

        (ecto 3.4.5) lib/ecto/repo/schema.ex:700: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
        (elixir 1.10.4) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
        (ecto 3.4.5) lib/ecto/repo/schema.ex:685: Ecto.Repo.Schema.constraints_to_errors/3
        (ecto 3.4.5) lib/ecto/repo/schema.ex:666: Ecto.Repo.Schema.apply/4
        (ecto 3.4.5) lib/ecto/repo/schema.ex:263: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
        (rocketship 0.1.0) lib/rocketship_web/controllers/parent_organization_user_controller.ex:21: RocketshipWeb.ParentOrganizationUserController.create/2
        (rocketship 0.1.0) lib/rocketship_web/controllers/parent_organization_user_controller.ex:1: RocketshipWeb.ParentOrganizationUserController.action/2
        (rocketship 0.1.0) lib/rocketship_web/controllers/parent_organization_user_controller.ex:1: RocketshipWeb.ParentOrganizationUserController.phoenix_controller_pipeline/2

CONTROLLER

def create(conn, %{"parent_organization_user" => parent_organization_user_params, "parent_organization_id" => parent_organization_id}) do
    parent_organization_user_params = Map.put(parent_organization_user_params, "parent_organization_id", parent_organization_id)
    case Organizations.create_parent_organization_user(parent_organization_user_params) do
      {:ok, parent_organization_user} ->
        conn
        |> put_flash(:info, "Parent organization user created successfully.")
        |> redirect(to: Routes.parent_organization_user_path(conn, :index, parent_organization_id))

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

SCHEMA

defmodule Rocketship.Organizations.ParentOrganizationUser do
  use Ecto.Schema
  import Ecto.Changeset

  schema "parent_organizations_users" do
    field :role, :string
    
    belongs_to :user, Rocketship.Accounts.User
    belongs_to :parent_organization, Rocketship.Organizations.ParentOrganization

    timestamps()
  end

  @doc false
  def changeset(parent_organization_user, attrs) do
    parent_organization_user
    |> cast(attrs, [:role, :parent_organization_id, :user_id])
    |> validate_required([:role, :parent_organization_id, :user_id])
  end
end

Marked As Solved

Owens

Owens

Hello all, I found the mistake, I had a typo in my migration file.

The typo

add :parent_organization_id, references(:organizations, on_delete: :delete_all)

Should be references(:parent_organizations

The reason it worked on local but not production is because I hadn’t created organizations yet.

Thanks @jesse for helping debug this.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement