Skip to content

[BUG] Unknown expression or function identifier default in scope SELECT replica_num FROM system.clusters WHERE cluster = default #138

@estromenko

Description

@estromenko

Describe the bug

When I run migrations with migration_cluster and distributed_migrations features (not available in official release yet), I receive an error.

django.db.utils.OperationalError: Code: 47. DB::Exception: Unknown expression or function identifier 'default' in scope SELECT replica_num FROM system.clusters WHERE cluster = default. Stack trace:

To Reproduce

Sample config:

DATABASES = {
    "clickhouse": {
        "ENGINE": "clickhouse_backend.backend",
        "NAME": os.getenv("CLICKHOUSE_NAME", "default"),
        "HOST": os.getenv("CLICKHOUSE_HOST", "localhost"),
        "PORT": os.getenv("CLICKHOUSE_PORT", "9000"),
        "USER": os.getenv("CLICKHOUSE_USER", "default"),
        "PASSWORD": os.getenv("CLICKHOUSE_PASSWORD", ""),
        "OPTIONS": {
            "migration_cluster": "default",
            "distributed_migrations": True,
        },
        "TEST": {"cluster": "default"},
    },
}

To run clickhouse locally in cluster mode:

kind create cluster
helm upgrade --install clickhouse clickhouse --repo https://helm.altinity.com --version 0.3.1 \
  --set keeper.enabled=true --set clickhouse.defaultUser.allowExternalAccess=true
kubectl port-forward svc/clickhouse-clickhouse 9000:9000

Sample command to run migrations:

uv run --env-file .env manage.py migrate --database clickhouse

Expected behavior

Seems like the proment in this part of code (missing ' around cluster name):

def _check_replicas(connection):
    """
    Check if the connection has replicas configured for the migration cluster.
    """
    if hasattr(connection, "has_replicas"):
        return connection.has_replicas

    with connection.cursor() as cursor:
        cursor.execute(
            f"select replica_num from system.clusters where cluster={connection.migration_cluster}"
        )
        (replica_count,) = cursor.fetchone()
    return replica_count >= 1

cluster={connection.migration_cluster} -> cluster='{connection.migration_cluster}'

Versions

  • ClickHouse server version - 25.3.6.10034.altinitystable
  • Python version - 3.13
  • Clickhouse-driver version - 0.2.9
  • Django version - 5.2
  • Django clickhouse backend version - https://github.com/jayvynl/django-clickhouse-backend#03b9adb4d27919c3e3e197d48348c89c0d4a7f62

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions