Ecto unique_index

Hello!
I’ve a table called users and I would like to create unique indexes on two fields :phone and :email.

create unique_index(:users, [:phone, :email] and

create unique_index(:users, [:phone])
create unique_index(:users, [:email])
are one and the same?

Sorry, if I sound too naive, I’m still learning Ecto and Postgres, also I would be grateful if you could please recommend resources for learning to work with Postgres :slight_smile:

No, it’s not the same.

create unique_index(:users, [:phone, :email]

It will create a unique index where you cannot have a value with both the phone and email identical. For example, you could have two records with the same email, but not two records with the same email and the same phone.

create unique_index(:users, [:phone])
create unique_index(:users, [:email])

For this one, you can’t have two records with the same email, or two records with the same phone.

5 Likes

I think, I should use the above one, as I would like to have unique email & unique phone for the users.

Do you know any resource that could help me learn about working with Database’s especially Postgres?

I haven’t been around resources to learn that stuff in a long time. I’d say something like https://www.postgresqltutorial.com/ should do the job, at the very least to get you up and running with PostgreSQL. Of course, going down the road of learning from Postgres own documentation would be a good idea, but it might not be easy to start with.

Thank you sharing them, I appreciate it greatly.

I can’t recommend enough SQL Antipatterns, Volume 1: Avoiding the Pitfalls of Database Programming by Bill Karwin as a great guide for thinking about strucutes, etc. with very concrete examples.

:honeybee: :honeybee: :honeybee:

3 Likes

I would also like to recommend the The Art of Postgresql by Dimitri Fontaine.

2 Likes

Thank you Hardik, take care.