# Create unique integer with ex_machina?

I am trying out ex_machina package – I do not see how to create unique integers however. Is this possible? My need is to simulate keys from foreign database. Thanks for advice!

There are probably better solutions, but I wonder if you could just use `System.unique_integer/1`?

2 Likes

I came up with this function:

``````  def sequence_int(sequence_name, start \\ 0) do
Agent.get_and_update(ExMachina.Sequence, fn sequences ->
current_value = Map.get(sequences, sequence_name, start)
new_sequences = Map.put(sequences, sequence_name, current_value + 1)
{current_value, new_sequences}
end)
end
``````

so I can do

``````def thing_factory
%{
foreign_id: sequence_int(:foreign_id, 100)
}
end
``````

I don’t know if this would be a useful feature for others

@vrod if you want sequence of integer (as I needed today), you can use this, which will just return the integer value unmodified:

``````%{
foreign_id: sequence("foreign_id", & &1)
}
``````