I’ve got a very weird error happening which I hope someone smarter than me can figure out
The thing is that I’m having trouble inserting some simple data on my production environment.
Here’s the code
defp do_create_pull_request(remote_pull_request, board) do
Ecto.Multi.new
|> insert_pull_request(remote_pull_request, board)
|> Repo.transaction
end
defp insert_pull_request(multi, remote_pull_request, board) do
Ecto.Multi.run(multi, :pull_request, fn(_) ->
attrs = %{
title: remote_pull_request.title,
description: remote_pull_request.description,
remote_id: remote_pull_request.id
}
git_repository = Pace.GitRepositories.find_by_uuid_in_board(
remote_pull_request.repository_uuid,
board
)
Pace.PullRequest.changeset(%Pace.PullRequest{}, attrs)
|> Ecto.Changeset.put_assoc(:git_repository, git_repository)
|> Repo.insert
end)
end
I’ve removed other operations in the Multi to simplify the code. This code does result in a validation error on the PullRequest.
Here’s the schema for the PullRequest
defmodule Pace.PullRequest do
use Pace.Web, :model
@timestamps_opts [type: :utc_datetime]
schema "pull_requests" do
field :remote_id, :integer
field :title, :string
field :description, :string
field :state, :string
belongs_to :git_repository, Pace.GitRepository
has_many :pull_request_assignees, Pace.PullRequestAssignee
has_many :assignees, through: [:pull_request_assignees, :board_member]
has_many :issue_pull_requests, Pace.IssuePullRequest
has_many :cards, through: [:issue_pull_requests, :card]
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:remote_id, :title, :description, :state])
|> validate_required([:remote_id, :title])
end
end
When running the insert code shown in the beginning I get the following validation error:
#Ecto.Changeset<action: :insert,
changes: %{description: "references #140", git_repository_id: 12, id: "",
remote_id: 97, title: "new readme test"},
errors: [issue_pull_requests: "is invalid"], data: #Pace.PullRequest<>,
valid?: false>
Since I am not doing anything related to the issue_pull_requests
this makes no sense. But let me just make things even more weird
Because it turns out, if I construct an Ecto.Multi
chain in iex on production it does work.
So doing the following on production iex:
multi = Ecto.Multi.new
multi = Ecto.Multi.run(multi, :pull_request, fn(_) ->
attrs = %{
title: remote_pull_request.title,
description: remote_pull_request.description,
remote_id: remote_pull_request.id
}
git_repository = Pace.GitRepositories.find_by_uuid_in_board(
remote_pull_request.repository_uuid,
board
)
Pace.PullRequest.changeset(%Pace.PullRequest{}, attrs)
|> Ecto.Changeset.put_assoc(:git_repository, git_repository)
|> Repo.insert
end)
Repo.transaction(multi)
Which is the same code. I just removed the named functions. Data is the same and all. Then it works as expected. What is going on?