0

I would like to prevent duplicate column values from being created within a certain time period, say somewhere between 1-60 seconds. Outside of that time window, the columns don't have have to be unique.

Is there a way to add a constraint on a couple mysql database columns and a datetime column +- 30 seconds.

Patrick Cullen
  • 8,654
  • 3
  • 21
  • 23
  • Are you thinking of this in 'windows', that is each and every minute can only have one booking, e.g. you can have booking at 1:59 and another at 2:01, or that in case of each booking no booking can be done 30 seconds before or 30 seconds after? – Eyeslandic Feb 16 '17 at 10:10
  • 1
    May [this](http://guides.rubyonrails.org/active_record_validations.html#custom-validators) helps – Weibo Chen Feb 16 '17 at 14:05
  • Validators have known concurrency problems: http://stackoverflow.com/questions/22814122/rails-validate-uniqueness-fails-on-concurrent-inputs – Patrick Cullen Feb 16 '17 at 18:21
  • @Iceman - For each booking, no booking 30 seconds before or after. As far as 'windows' go, I can't get past the problem that transactions that occur near the boundary but in different windows could be separated by less than a second. I'm thinking there may be a way to record the last duplicate, and the time since in minutes and enforce 2 constraints, one on the size of time since and another on uniqueness of id and time since, but this feels hokey. – Patrick Cullen Feb 16 '17 at 18:29

0 Answers0