You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Django clickhouse backend is a [django database backend](https://docs.djangoproject.com/en/4.1/ref/databases/) for
7
+
[clickhouse](https://clickhouse.com/docs/en/home/) database. This project allows using django ORM to interact with
8
+
clickhouse.
9
+
10
+
Thanks to [clickhouse driver](https://github.com/mymarilyn/clickhouse-driver), django clickhouse backend use it as [DBAPI](https://peps.python.org/pep-0249/).
11
+
Thanks to [clickhouse pool](https://github.com/ericmccarthy7/clickhouse-pool), it makes clickhouse connection pool.
12
+
13
+
14
+
**features:**
15
+
16
+
- Support [Clickhouse native interface](https://clickhouse.com/docs/en/interfaces/tcp/) and connection pool.
17
+
- Define clickhouse specific schema features such as [Engine](https://clickhouse.com/docs/en/engines/table-engines/) and [Index](https://clickhouse.com/docs/en/guides/improving-query-performance/skipping-indexes) in django ORM.
18
+
- Support table migrations.
19
+
- Support creating test database and table, working with django TestCase and pytest-django.
20
+
- Support most types of query and data types, full feature is under developing.
21
+
- Support [SETTINGS in SELECT Query](https://clickhouse.com/docs/en/sql-reference/statements/select/#settings-in-select-query).
Writing testcase is all the same as normal django project. You can use django TestCase or pytest-django.
125
+
**Notice:** clickhouse use mutations for [deleting or updating](https://clickhouse.com/docs/en/guides/developer/mutations).
126
+
By default, data mutations is processed asynchronously, so you should change this default behavior in testing for deleting or updating.
127
+
There are 2 ways to do that:
128
+
129
+
- Config database engine as follows, this sets [`mutations_sync=1`](https://clickhouse.com/docs/en/operations/settings/settings#mutations_sync) at session scope.
130
+
```python
131
+
DATABASES= {
132
+
'default': {
133
+
'ENGINE': 'clickhouse_backend.backend',
134
+
'OPTIONS': {
135
+
'settings': {
136
+
'mutations_sync': 1,
137
+
}
138
+
}
139
+
}
140
+
}
141
+
```
142
+
- Use [SETTINGS in SELECT Query](https://clickhouse.com/docs/en/sql-reference/statements/select/#settings-in-select-query).
Django ORM depends heavily on single column primary key, this primary key is a unique identifier of an ORM object.
163
+
All `get``save``delete` actions depend on primary key.
164
+
165
+
But in ClickHouse [primary key](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#primary-keys-and-indexes-in-queries) has different meaning with django primary key. ClickHouse does not require a unique primary key. You can insert multiple rows with the same primary key.
166
+
167
+
There is [no unique constraint](https://github.com/ClickHouse/ClickHouse/issues/3386#issuecomment-429874647) or auto increasing column in clickhouse.
168
+
169
+
By default, django will add a field named `id` as auto increasing primary key.
170
+
171
+
- AutoField
172
+
173
+
Mapped to clickhouse Int32 data type. You should generate this unique id yourself
174
+
175
+
- BigAutoField
176
+
177
+
Mapped to clickhouse Int64 data type. If primary key is not specified when insert data, then `clickhouse_driver.idworker.id_worker` is used to generate this unique key.
178
+
179
+
Default id_worker is an instance of `clickhouse.idworker.snowflake.SnowflakeIDWorker` which implement [twitter snowflake id](https://en.wikipedia.org/wiki/Snowflake_ID).
180
+
If data insertions happen on multiple datacenter, server, process or thread, you should ensure uniqueness of (CLICKHOUSE_WORKER_ID, CLICKHOUSE_DATACENTER_ID) environment variable.
181
+
Because work_id and datacenter_id are 5 bits, they should be an integer between 0 and 31. CLICKHOUSE_WORKER_ID default to 0, CLICKHOUSE_DATACENTER_ID will be generated randomly if not provided.
182
+
183
+
`clickhouse.idworker.snowflake.SnowflakeIDWorker` is not thread safe. You could inherit `clickhouse.idworker.base.BaseIDWorker` and implement one, and set `CLICKHOUSE_ID_WORKER` to doted import path of your IDWorker instance.
184
+
185
+
Django use a table named `django_migrations` to track migration files. ID field should be BigAutoField, so that IDWorker can generate unique id for you.
186
+
After Django 3.2,a new [config `DEFAULT_AUTO_FIELD`](https://docs.djangoproject.com/en/4.1/releases/3.2/#customizing-type-of-auto-created-primary-keys) is introduced to control field type of default primary key.
187
+
So `DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'` is required if you want to use migrations with django clickhouse backend.
188
+
189
+
190
+
### Fields
191
+
192
+
#### Nullable
193
+
194
+
`null=True` will make [Nullable](https://clickhouse.com/docs/en/sql-reference/data-types/nullable/) type in clickhouse database.
195
+
196
+
**Note** Using Nullable almost always negatively affects performance, keep this in mind when designing your databases.
197
+
198
+
#### GenericIPAddressField
199
+
200
+
Clickhouse backend has its own implementation in `clickhouse_backend.models.fields.GenericIPAddressField`.
201
+
If `protocol='ipv4'`, a column of [IPv4](https://clickhouse.com/docs/en/sql-reference/data-types/domains/ipv4) is generated, else [IPv6](https://clickhouse.com/docs/en/sql-reference/data-types/domains/ipv6) is generated.
202
+
203
+
#### PositiveSmallIntegerField
204
+
#### PositiveIntegerField
205
+
#### PositiveBigIntegerField
206
+
207
+
`clickhouse_backend.models.fields.PositiveSmallIntegerField` maps to [UInt16](https://clickhouse.com/docs/en/sql-reference/data-types/int-uint).
208
+
`clickhouse_backend.models.fields.PositiveIntegerField` maps to [UInt32](https://clickhouse.com/docs/en/sql-reference/data-types/int-uint).
209
+
`clickhouse_backend.models.fields.PositiveBigIntegerField` maps to [UInt64](https://clickhouse.com/docs/en/sql-reference/data-types/int-uint).
210
+
Clickhouse have unsigned integer type, these fields will have right integer range validators.
0 commit comments