Ash FrameworkAF
Ash Framework3y ago
7 replies
Francis

AshCsv - duplication on `upsert?: true`

I might be doing something wrong but when using AshCsv with upsert?: true on a create action, I get duplicates in the CSV file. For context, here's the ressource

elixir

defmodule WorkoutManager.Exercises.Equipment do
  @moduledoc false

  alias WorkoutManager.Exercises

  use Ash.Resource,
    data_layer: AshCsv.DataLayer

  csv do
    file "priv/exercises/equipment.csv"
    create? true
    header? true
    columns [:name]
  end

  attributes do
    attribute :name, :ci_string do
      allow_nil? false
      primary_key? true
      constraints casing: :lower
    end
  end

  actions do
    defaults [:read, :destroy]

    create :upsert do
      accept [:name]
      upsert? true
    end
  end

  code_interface do
    define_for Exercises

    define :read_all, action: :read
    define :upsert, args: [:name]
    define :destroy
  end
end


This happens when I call WorkoutManager.Exercises.Equipment.upsert!("rings") multiple times. The behaviour I was expecting was that if rings was already in the file, it wouldn't duplicate it but it inserts a duplicate.
Was this page helpful?