django-clickhouse-backend icon indicating copy to clipboard operation
django-clickhouse-backend copied to clipboard

[BUG] Didn't support database alias

Open ramwin opened this issue 6 months ago • 2 comments

Describe the bug cannot migrate

To Reproduce git clone https://github.com/ramwin/schoolproject.git cd schoolproject # install clickhouse_backend

# in schoolproject/database_settings.py add 
"clickhouse": {
        "ENGINE": "clickhouse_backend.backend",
        "PASSWORD": "wangxiang",
}
# in schoolproject/settings.py add
INSTALLED_APPS = [
   ...
   "clickhouse_backend"
]

python3 manage.py migrate --database clickhouse

Expected behavior A clear and concise description of what you expected to happen. the migration works

Versions

  • ClickHouse server version. Version can be obtained by running SELECT version() query.
  • Python version. 3.10.12
  • Clickhouse-driver version. 25.5.2.47
  • Django version. 5.1.6
  • Django clickhouse backend version. 1.4

ramwin avatar Jun 10 '25 12:06 ramwin

thank you for the report . i will check soon .

jayvynl avatar Jun 12 '25 01:06 jayvynl

Because I am unable to start your project because I don't have some of you local files.

I use all the versions as you listed, but unable to reproduce the problem.

Python version. 3.10.12
Clickhouse-driver version. 25.5.2.47
Django version. 5.1.6
Django clickhouse backend version. 1.4

These are my steps:

django-admin startproject test_dcb
cd test_dcb
python manage.py startapp student
# add student app to test_dcb/settings.py

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "student",
]

# add database setting to test_dcb/settings.py
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    },
    "ch": {
        "ENGINE": "clickhouse_backend.backend",
        "PASSWORD": "clickhouse_password",
        "NAME": "test",
        "OPTIONS": {
            "settings": {
                "allow_experimental_object_type": 1,
            }
        },
        "TEST": {"MIGRATE": False},
    }
}
# student/models.py
from django.db import models


class Student(models.Model):
    name = models.CharField(max_length=32)
    no = models.CharField(max_length=32)
python manage.py migrate --database ch
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, student
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial.../home/wen/Projects/django-clickhouse-backend/clickhouse_backend/backend/schema.py:217: UserWarning: Because index requires extra params, such as TYPE and GRANULARITY, so field level index=True and Meta level index_together is ignored. Refer to https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree/#table_engine-mergetree-data_skipping-indexes
  warnings.warn(msg)
 OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
  Applying student.0001_initial... OK

Inspect tables:

docker exec -it clickhouse-node1 clickhouse-client --database test
ClickHouse client version 25.5.2.47 (official build).
Connecting to database test at localhost:9000 as user default.
Connected to ClickHouse server version 25.5.2.

Warnings:
 * Delay accounting is not enabled, OSIOWaitMicroseconds will not be gathered. You can enable it using `echo 1 > /proc/sys/kernel/task_delayacct` or by using sysctl.

f476fff08c2f :) select * from django_migrations limit 10

SELECT *
FROM django_migrations
LIMIT 10

Query id: b26713b0-6e2d-456b-8ecb-21988500d3d3

    ┌──────────────────id─┬─app───┬─name─────────────────────────────────────┬────────────────────applied─┬─deleted─┐
 1. │ 1933157245706960896 │ admin │ 0001_initial                             │ 2025-06-12 13:39:38.358004 │ false   │
 2. │ 1933157245803429888 │ admin │ 0002_logentry_remove_auto_add            │ 2025-06-12 13:39:38.381661 │ false   │
 3. │ 1933157245895704576 │ admin │ 0003_logentry_add_action_flag_choices    │ 2025-06-12 13:39:38.403547 │ false   │
 4. │ 1933157245585326080 │ auth  │ 0001_initial                             │ 2025-06-12 13:39:38.329133 │ false   │
 5. │ 1933157246256414720 │ auth  │ 0002_alter_permission_name_max_length    │ 2025-06-12 13:39:38.489049 │ false   │
 6. │ 1933157246369660928 │ auth  │ 0003_alter_user_email_max_length         │ 2025-06-12 13:39:38.516749 │ false   │
 7. │ 1933157246470324224 │ auth  │ 0004_alter_user_username_opts            │ 2025-06-12 13:39:38.540836 │ false   │
 8. │ 1933157246608736256 │ auth  │ 0005_alter_user_last_login_null          │ 2025-06-12 13:39:38.573881 │ false   │
 9. │ 1933157246663262208 │ auth  │ 0006_require_contenttypes_0002           │ 2025-06-12 13:39:38.586822 │ false   │
10. │ 1933157246776508416 │ auth  │ 0007_alter_validators_add_error_messages │ 2025-06-12 13:39:38.612999 │ false   │
    └─────────────────────┴───────┴──────────────────────────────────────────┴────────────────────────────┴─────────┘

10 rows in set. Elapsed: 0.003 sec. 

f476fff08c2f :) \d

SHOW TABLES

Query id: 2f8e98e6-cd98-481f-947b-00bfae038909

    ┌─name───────────────────────┐
 1. │ auth_group                 │display of the progress table.
 2. │ auth_group_permissions     │
 3. │ auth_permission            │
 4. │ auth_user                  │
 5. │ auth_user_groups           │
 6. │ auth_user_user_permissions │
 7. │ django_admin_log           │
 8. │ django_content_type        │
 9. │ django_migrations          │
10. │ django_session             │
11. │ student_student            │
    └────────────────────────────┘

11 rows in set. Elapsed: 0.006 sec. 

jayvynl avatar Jun 12 '25 13:06 jayvynl