fireproofsocks

fireproofsocks

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!

Showing Posts 1 to 2

halostatue

halostatue

You’re going to want to make sure that you create unique_index(:code_table, [:code]) in your migration, and then you just need to make sure that when you create an Ecto.Changeset, you have something that will put a generated code if it doesn’t already exist.

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews