Skip to main content
ClickHouse Connect includes the clickhousedb SQLAlchemy dialect on top of the core driver. It supports SQLAlchemy 1.4.40 and later, including SQLAlchemy 2.x, with a focus on Core queries, ClickHouse DDL, reflection, and simple ORM inserts. Install the SQLAlchemy dependencies with the package extra:

Connect with SQLAlchemy

Create an engine with either the clickhousedb:// or clickhousedb+connect:// URL form:
URL query parameters can contain ClickHouse settings, ClickHouse Connect client options such as compression, query_limit, and timeouts, or HTTP/TLS options such as ca_cert. Prefix a ClickHouse setting with ch_ to force it to be treated as a server setting when needed, for example ch_http_max_field_name_size=99999. See Connection arguments and settings for the available client options.

Per-query settings

Pass ClickHouse settings through SQLAlchemy execution options. Settings can be set on an engine, connection, or statement. A statement value takes precedence over a connection or engine value with the same key.

Server-side parameters

SQLAlchemy normally renders client-side parameters. Opt in to ClickHouse server-side parameters when creating the engine:
In this mode every bound value must have a ClickHouse-compatible SQLAlchemy type. Supported IN lists become typed ClickHouse Array parameters. The compiler raises CompileError when it cannot derive a compatible type or safely process a bind.

Core queries

The dialect supports SQLAlchemy Core SELECT queries with joins, filters, ordering, limits and offsets, and DISTINCT.
Lightweight DELETE is supported and requires an explicit WHERE clause:

ClickHouse query extensions

Import select from clickhouse_connect.cc_sqlalchemy to expose typed ClickHouse methods to static type checkers. The standard sqlalchemy.select also has these methods at runtime.
The ClickHouse Select methods are: For example, a ClickHouse GLOBAL ANY LEFT JOIN can be chained without nesting a custom FromClause:
Use the explicit Lambda construct for ClickHouse higher-order functions:
The standard SQLAlchemy values() construct compiles to ClickHouse’s VALUES table-function syntax, including when used in a common table expression (the CTE form requires SQLAlchemy 2.x, where Values.cte() was added).

DDL and reflection

ClickHouse Connect provides ClickHouse data types, table engines, dictionary constructs, database DDL, and table reflection.
Reflected columns carry server_default for DEFAULT expressions and dialect-specific attributes such as clickhouse_codec, clickhouse_ttl, clickhouse_materialized, and clickhouse_alias when present. MergeTree key arguments such as order_by, partition_by, primary_key, sample_by, and ttl accept SQLAlchemy column and SQL expressions as well as plain strings.

Inserts and basic ORM use

Core inserts and simple ORM models are supported. Prefer Core inserts for bulk data paths.

Alembic migrations

ClickHouse Connect includes Alembic integration for ClickHouse schema migrations. Install it with:
Import clickhouse_connect.cc_sqlalchemy.alembic in Alembic’s env.py to register the dialect integration. Autogenerate supports common table evolution, including table creation and removal, column add/alter/drop, defaults, and comments. Use manual operations for table and column renames. Review every generated migration before applying it. ClickHouse-specific op.* helpers cover:
  • Data skipping indexes, including add, materialize, and drop operations.
  • Projections, including add, materialize, and drop operations.
  • MergeTree table setting modification and reset.
  • Materialized view creation and removal.
  • Dictionary creation, removal, and reload.
ClickHouse data skipping indexes are not SQLAlchemy indexes. Index, Column(index=True), op.create_index, and op.drop_index are rejected to avoid partial or incorrect DDL. Use op.add_clickhouse_index and op.drop_clickhouse_index. See the complete Alembic worked example. Users migrating from clickhouse-sqlalchemy should also read the migration guide.

Scope and limitations

  • ClickHouse does not provide traditional transactions through this HTTP dialect. engine.begin() and Session.commit() organize Python-side work, but commit and rollback are no-ops on the server.
  • UPDATE, two-phase transactions, sequences, RETURNING, and advanced isolation levels are not implemented by the dialect. Use explicit ClickHouse SQL for server mutations when needed.
  • Column(..., primary_key=True) supplies SQLAlchemy object identity. It does not create a server-side uniqueness constraint. Define sorting and optional primary-key expressions through the table engine.
  • Traditional foreign-key, unique-constraint, and standard index metadata are not available because ClickHouse does not enforce those constraints.
  • ORM relationship management, unit-of-work updates, cascades, and eager or lazy relationship loading are outside the supported ORM scope.
Last modified on July 23, 2026