Ecto: Create a case insensitive unique index

juffel

juffel

Posted on February 10, 2023

Ecto: Create a case insensitive unique index

Today I learned that Ecto.Migration supports this out of the box ðŸĨģ

You probably know that you can easily create a unique index like this:

create unique_index("products", [:name], name: :products_lower_name_index)
Enter fullscreen mode Exit fullscreen mode

But did you also know that you can do this also in a case-insensitive way?
According to the docs you can not only provide column names for a new unique index, but also a custom expression.

# Create an index on a custom expression
create unique_index("products", ["(lower(name))"], name: :products_lower_name_index)
Enter fullscreen mode Exit fullscreen mode
💖 💊 🙅 ðŸšĐ
juffel
juffel

Posted on February 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related