avoid key: has already been taken error on test fixtures

i have a test for a entity that has a enum column, example:

schema "cars" do
  field :manufacturer, Ecto.enum, values [:bmw, :volks]
end

and inside my factory i have:

defmodule MyApp.Factories.CarsFactory do
@moduledoc false

  defmacro __using__(_opts) do
    quote do
     alias MyApp.Cars.Car
     def car_factory do
       %Car{
            manufacturer: Car |> Ecto.Enum.values(:manufacturer) |> Enum.random()
          }
     end
    end
end

but when i run the tests i get key: has already been taken error on test fixtures
only on create/update

“has already been taken” sounds like Ecto’s unique constraint violation. This doesn’t seem to be related to the snippet you’ve shown. You’ll need to look into your changeset functions to see where this is coming from.

1 Like