pingcap/django-tidb

Support `AUTO_RANDOM`

wd0517 opened this issue · 4 comments

wd0517 commented

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

  1. Create a new class called BigAutoRandomField that inherits from the existing BigAutoField class. You can find the implementation of BigAutoField here.
  2. In the DatabaseWrapper, define the data type for this new field (BigAutoRandomField).
  3. Finally, in your project's settings.py, set the value of DEFAULT_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_INCREMENTs. Do we want to do anything with that?

wd0517 commented

Sure, I've created a new issue for tracking. I will follow up once I have completed my current tasks.