Django backend for TiDB
Project description
TiDB dialect for Django
This adds compatibility for TiDB to Django.
Installation Guide
Prerequisites
Before installing django-tidb, ensure you have a MySQL driver installed. You can choose either mysqlclient
(recommended) or pymysql
(at your own risk).
Install mysqlclient (Recommended)
Please refer to the mysqlclient official guide
Install pymysql (At your own risk)
django-tidb has not been tested with pymysql
pip install pymysql
Then add the following code at the beginning of your Django's settings.py
:
import pymysql
pymysql.install_as_MySQLdb()
Installing django-tidb
To install django-tidb, you need to select the version that corresponds with your Django version. Please refer to the table below for guidance:
The minor release number of Django doesn't correspond to the minor release number of django-tidb. Use the latest minor release of each.
django | django-tidb | install command |
---|---|---|
v4.2.x | v4.2.x | pip install 'django-tidb>=4.2.0,<4.3.0' |
v4.1.x | v4.1.x | pip install 'django-tidb>=4.1.0,<4.2.0' |
v3.2.x | v3.2.x | pip install 'django-tidb>=3.2.0,<3.3.0' |
Usage
Set 'ENGINE': 'django_tidb'
in your settings to this:
DATABASES = {
'default': {
'ENGINE': 'django_tidb',
'NAME': 'django',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 4000,
},
}
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
USE_TZ = False
SECRET_KEY = 'django_tests_secret_key'
Using AUTO_RANDOM
AUTO_RANDOM
is a feature in TiDB that generates unique IDs for a table automatically. It is similar to AUTO_INCREMENT
, but it can avoid write hotspot in a single storage node caused by TiDB assigning consecutive IDs. It also have some restrictions, please refer to the documentation.
To use AUTO_RANDOM
in Django, you can do it by following two ways:
-
Declare globally in
settings.py
as shown below, it will affect all models:DEFAULT_AUTO_FIELD = 'django_tidb.fields.BigAutoRandomField'
-
Manually declare it in the model as shown below:
from django_tidb.fields import BigAutoRandomField class MyModel(models.Model): id = BigAutoRandomField(primary_key=True) title = models.CharField(max_length=200)
BigAutoRandomField
is a subclass of BigAutoField
, it can only be used for primary key and its behavior can be controlled by setting the parameters shard_bits
and range
. For detailed information, please refer to the documentation.
Migrate from AUTO_INCREMENT
to AUTO_RANDOM
:
-
Check if the original column is
BigAutoField(bigint)
, if not, migrate it toBigAutoField(bigint)
first. -
In the database configuration (
settings.py
), defineSET @@tidb_allow_remove_auto_inc = ON
in theinit_command
. You can remove it after completing the migration.# settings.py DATABASES = { 'default': { 'ENGINE': 'django_tidb', ... 'OPTIONS': { 'init_command': 'SET @@tidb_allow_remove_auto_inc = ON', } } }
-
Finnaly, migrate it to
BigAutoRandomField(bigint)
.
Note
AUTO_RANDOM
is supported after TiDB v3.1.0, and only support define withrange
after v6.3.0, sorange
will be ignored if TiDB version is lower than v6.3.0
Using AUTO_ID_CACHE
AUTO_ID_CACHE
allow users to set the cache size for allocating the auto-increment ID, as you may know, TiDB guarantees that AUTO_INCREMENT values are monotonic (always increasing) on a per-server basis, but its value may appear to jump dramatically if an INSERT operation is performed against another TiDB Server, This is caused by the fact that each server has its own cache which is controlled by AUTO_ID_CACHE
. But from TiDB v6.4.0, it introduces a centralized auto-increment ID allocating service, you can enable MySQL compatibility mode by set AUTO_ID_CACHE
to 1
when creating a table without losing performance.
To use AUTO_ID_CACHE
in Django, you can specify tidb_auto_id_cache
in the model's Meta
class as shown below when creating a new table:
class MyModel(models.Model):
title = models.CharField(max_length=200)
class Meta:
tidb_auto_id_cache = 1
But there are some limitations:
tidb_auto_id_cache
can only affect the table creation, after that it will be ignored even if you change it.tidb_auto_id_cache
only affects theAUTO_INCREMENT
column.
Supported versions
- TiDB 4.0 and newer
- Django 3.2, 4.1 and 4.2
- Python 3.6 and newer(must match Django's Python version requirement)
Test
create your virtualenv with:
$ virtualenv venv
$ source venv/bin/activate
you can use the command deactivate
to exit from the virtual environment.
run all integration tests.
$ DJANGO_VERSION=3.2.12 python run_testing_worker.py
Migrate from previous versions
Releases on PyPi before 3.0.0 are published from repository https://github.com/blacktear23/django_tidb. This repository is a new implementation and released under versions from 3.0.0. No backwards compatibility is ensured. The most significant points are:
- Only Django 3.2 and 4.0 are tested and supported.
- Engine name is
django_tidb
instead ofdjango_tidb.tidb
.
Known issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-tidb-4.2.2.tar.gz
.
File metadata
- Download URL: django-tidb-4.2.2.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97314c5f92a9d069763b5e0ff7dd7e2c61ec906d95a1b1f2abbdf400df653026 |
|
MD5 | 7291b1d929268864bdeff24ed550e25c |
|
BLAKE2b-256 | b6c476c09e4b04fe82bd7f1763cbae4ccd582a24e0d2209adcee64ab6ff136c3 |
File details
Details for the file django_tidb-4.2.2-py3-none-any.whl
.
File metadata
- Download URL: django_tidb-4.2.2-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2f4002bed5749aa6b9ba52936be60ae6889d382e9a7ff3de6e0124e68af7eda |
|
MD5 | 2339eeff0f6f62ae0fc29a4438e5687f |
|
BLAKE2b-256 | c2b23617fc4786906c0afc0810353f61a42a55ddcc9a9abc62bc07157355b5b0 |