Skip to content

Clickhouse generation creating invalid python #469

@coby-astsec

Description

@coby-astsec

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Sqlacodegen version

4.0.2

SQLAlchemy version

2.0.48

RDBMS vendor

Other

What happened?

Invalid python is generated when parsing the given clickhouse DB schema

from typing import Any

from clickhouse_sqlalchemy.types.common import Array, UUID
from sqlalchemy import text
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.sql.sqltypes import NullType

class Base(DeclarativeBase):
    pass


class Testtable(Base):
    __tablename__ = 'testtable'

    id: Mapped[str] = mapped_column(UUID, primary_key=True) # This should be uuid.UUID
    col1: Mapped[list] = mapped_column(Array(<class 'clickhouse_sqlalchemy.types.common.String'>), nullable=False)
    col2: Mapped[list] = mapped_column(Array(<class 'clickhouse_sqlalchemy.types.common.String'>), nullable=False, server_default=text('[]'))
    col3: Mapped[Any] = mapped_column(NullType, nullable=False) # NOTE - This is also weird as it should be JSON
    col4: Mapped[list] = mapped_column(Array(<class 'sqlalchemy.sql.sqltypes.NullType'>), nullable=False)
    col5: Mapped[list] = mapped_column(Array(<class 'clickhouse_sqlalchemy.types.common.UUID'>), nullable=False)
    col6: Mapped[list] = mapped_column(Array(<class 'clickhouse_sqlalchemy.types.common.Int32'>), nullable=False)

There are also warnings regarding the JSON type:

/Users/user/.cache/uv/archive-v0/aS1Ku1BPpSOILuRQTOKY0/lib/python3.14/site-packages/clickhouse_sqlalchemy/drivers/base.py:309: SAWarning: Did not recognize type 'JSON' of column 'col3'
  warn("Did not recognize type '%s' of column '%s'" %
/Users/user/.cache/uv/archive-v0/aS1Ku1BPpSOILuRQTOKY0/lib/python3.14/site-packages/clickhouse_sqlalchemy/drivers/base.py:309: SAWarning: Did not recognize type 'JSON' of column 'col4'
  warn("Did not recognize type '%s' of column '%s'" %

Database schema for reproducing the bug

DROP TABLE IF EXISTS testtable;
CREATE TABLE IF NOT EXISTS testtable
(
    id UUID,
    col1 Array(String),
    col2 Array(String) DEFAULT [],
    col3 JSON,
    col4 Array(JSON),
    col5 Array(UUID),
    col6 Array(Int32)

) ENGINE = MergeTree()
ORDER BY (id);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions