Support `AUTO_RANDOM`
wd0517 opened this issue · 4 comments
auto_random
is a feature in TiDB that generates unique IDs for a table automatically. It is similar to auto_increment
in MySQL, but it can avoid the write hotspot issue caused by auto_increment
.
How
- Create a new class called
BigAutoRandomField
that inherits from the existingBigAutoField
class. You can find the implementation ofBigAutoField
here. - In the
DatabaseWrapper
, define the data type for this new field (BigAutoRandomField
). - Finally, in your project's
settings.py
, set the value ofDEFAULT_AUTO_FIELD
to'django_db.fields.BigAutoRandomField'
. This will enable the auto_random feature.
@zhangyangyu @dveeden PTAL
LGTM
Yes that sounds like good plan.
Note that AUTO_RANDOM
was causing issues with NodeJS applications as the Number type in Javascript doesn't like numbers of more than 54 bits. For this AUTO_RANDOM
was made to support AUTO_RANDOM(5, 54)
to restrict the numeric range.
For Python the full 64-bit numeric range (the default) should be fine. So I don't think we have to change anything here, but just so you're aware of this.
It is possible now to use AUTO_ID_CACHE=1
to have MySQL-compatible AUTO_INCREMENT
s. Do we want to do anything with that?