We also face problems with auto-incrementation bec...
# exposed
w
We also face problems with auto-incrementation because we need it to work within a specified range of numbers like 1000-9999. At the moment we have a bad workaround which really is a scuffed auto incrementation implemented by ourselfs. It looks like this:
Copy code
integer("Customer_Id").check { it.between(1000, 9999) }.uniqueIndex().entityId().also {
    it.defaultValueFun = {
        val incrementId = (1000 + Customer.count() + 1).toInt()
        EntityIDFunctionProvider.createEntityID(incrementId, this)
    }
}
How would one use auto-incrementation within a specified range? I appreciate any help!
a
You might have to just look up the latest row and increment off of that. But then you have to perform some sort of lock, or face the potential for race conditions. Or there might be some stored procedures you can use at the database level to generate the id. This wouldn't be handled by Exposed.
w
We already pretty much do that with
1000 + Customer.count() + 1
and the check
check { it.between(1000, 9999) }
acts as the lock. I thought there would be a safer overall better solution by exposed 😕
a
That solution is susceptible to collision if a row is deleted, and is still susceptible to a race condition when two rows are being added at the same time (provided you don't have a locking mechanism)
For example, if you have rows
[1001, 1002, 1003
], and delete row
1001
, then the next generated would be
1000 + 2 + 1
=
1003
, which collides with an existing record