Today I Learned

hashrocket A Hashrocket project

Conditional unique index with Ecto

I was going to create a conditional index using raw sql like this:

execute("""
create unique index posts_name_index on posts (name) where deleted_at is null;
""")

But then I learned that you can use the where option with create unique_index:

create unique_index(:posts, [:name], where: "deleted_at is null")
See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.