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 Ecto.Schema — Ecto v3.14.0 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

mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement