DEV Community

juffel
juffel

Posted on

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

Top comments (0)