fireproofsocks

fireproofsocks

Generate "simple" alphanumeric keys (unique)

I’m trying to define an Ecto table schema that includes a column that is used to uniquely identify rows but is NOT the primary key. Think of a short coupon code.

Although I could use PostGres’ UUID or GUID feature, I would prefer to use a shorter key for 2 reasons:

  1. They take up less memory. A smaller string requires less memory.
  2. It is extremely inconvenient to rely on huge UUID or GUID keys – think of the pain they will cause customer service who must refer to each record with those long-winded strings.

I’m not seeing any information in https://hexdocs.pm/ecto/Ecto.Schema.html that explains how to add a key to column, let alone something that demonstrates how to set up a custom mutator that would let me generate a unique value for a column. Can anyone let me know what I missed? Thanks!

Most Liked

voughtdq

voughtdq

@halostatue has answered your first question. Now as to your other question – about the keys. I’ve had success with the custom_base library. It generates code for a custom BaseN encoder/decoder.

defmodule MyBase do
  use CustomBase, ~c(0123456789abcdefghijklmnopqrstuvwxyz) # <-- you can customize these chars
                                                           # notice it is NOT a string!

  def encode(integer)

  def decode(binary)

  def decode!(binary)
end

The nice thing about this one is that you can continue to use integer keys (so actually you wouldn’t have to change much). When looking up the coupon code, you’d just decode it.

In this example, our coupon code is “jjp” which would come from the row id 25333:

iex(1)> MyBase.encode(25333)
"jjp"
iex(2)> MyBase.decode!("jjp")
25333

There might be concern about someone finding this pattern, so you could use a different order for the characters:

~c(a3oqmcylhd0w81v642eipzr9b5g7tufknsjx)

Or, if these coupon codes will be spoken over the phone, you might remove certain characters that can be misheard (i.e. b, c, d, e, p, t, v, and z are easy to mishear because they have the /iː/ sound at the end).

~c(0123456789adfhijklmoqruwxy)

Of course keeping in mind that you can shuffle the characters to avoid any exploitation/prediction of the next coupon code in a sequence.

Finally - you have to be careful with this method. You will have to maintain it for the life of the youngest coupon code generated using this method.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

We're in Beta

About us Mission Statement