Replacing sequence with random number

前端 未结 6 1576
被撕碎了的回忆
被撕碎了的回忆 2020-12-28 19:28

I would like to replace some of the sequences I use for id\'s in my postgresql db with my own custom made id generator. The generator would produce a random number with a ch

6条回答
  •  庸人自扰
    2020-12-28 20:04

    If you're not using this too often (you do not have a new customer every second, do you?) then it is feasible to just get a random number and then try to insert the record. Just be prepared to retry inserting with another number when it fails with unique constraint violation.

    I'd use numbers 1000000 to 999999 (900000 possible numbers of the same length) and check digit using UPC or ISBN 10 algorithm. 2 check digits would be better though as they'll eliminate 99% of human errors instead of 9%.

提交回复
热议问题