1

I have a table event_logs with two columns of interest for which I would like to create an index. The problem is one of the columns is a datetime field, which is fine for 99% of the things we use it for as it stores both the date and time for a given event.

However I would like to create a unique index which would make it impossible to create two events on the same date (not datetime, but just the date) with the same person.

  • quiz_master_id (integer)
  • start_at (datetime)

Is it even possible to do such a think to create such a unique index for a given date instead of datetime? Or is there another similar method to deal with this in Postgres?

1 Answer 1

2

Yes, you just want an index on an expression:

create unique index idx_eventlogs_startatdate
    on event_logs(date_trunc('day', start_at))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.