Today I learned a valuable lesson about how a seemingly simple task can have very rough edge cases, which take hours to solve. It involved Ecto, its associations and on_replace
option, and uniqueness checks in the database. Here’s the story.
The problem
Let’s say you are modelling some kind of processes. These processes have steps and the steps have to be executed in a precise order. This is how a database structure for it would look like:
def change do
create table(:processes) do
add :name, :string, null: false
end
create table(:steps) do
add :process_id, references(:processes)
add :name, :string, null: false
add :order, :integer, null: false
end
end
It is…