Skip to content

feat: Add optional sqlalchemy_use_enum for dy.Enum#355

Open
Jack Oberman (jackoberman) wants to merge 2 commits into
Quantco:mainfrom
jackoberman:feat/enum-sqlalchemy-use-enum
Open

feat: Add optional sqlalchemy_use_enum for dy.Enum#355
Jack Oberman (jackoberman) wants to merge 2 commits into
Quantco:mainfrom
jackoberman:feat/enum-sqlalchemy-use-enum

Conversation

@jackoberman
Copy link
Copy Markdown

Allow dy.Enum to emit sqlalchemy.Enum for to_sqlalchemy_columns when sqlalchemy_use_enum=True, with optional sqlalchemy_enum_name and column- name defaults for PostgreSQL native enum types.

Closes #354

Allow dy.Enum to emit sqlalchemy.Enum for to_sqlalchemy_columns when
sqlalchemy_use_enum=True, with optional sqlalchemy_enum_name and column-
name defaults for PostgreSQL native enum types.

Closes Quantco#354

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings June 2, 2026 22:56
@github-actions github-actions Bot added the enhancement New feature or request label Jun 2, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds optional support for generating native SQLAlchemy ENUM types from dy.Enum columns (primarily for PostgreSQL / Alembic drift detection), plus accompanying docs and tests.

Changes:

  • Extend dy.Enum with sqlalchemy_use_enum and sqlalchemy_enum_name flags to emit sqlalchemy.Enum instead of CHAR/VARCHAR.
  • Add tests for SQLAlchemy column/type generation and for (de)serialization/matching behavior of the new flags.
  • Document how to enable native SQL enums and how enum type names are chosen.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
dataframely/columns/enum.py Implements SQLAlchemy native enum mapping and naming rules.
tests/columns/test_sqlalchemy_columns.py Verifies dialect-specific compilation/type naming and error behavior.
tests/column_types/test_enum.py Validates matches() semantics + as_dict/from_dict for new flags.
docs/guides/features/sql-generation.md Documents how to use the new native SQL enum feature.

Comment on lines +109 to +119
def sqlalchemy_column(self, name: str, dialect: sa.Dialect) -> sa.Column:
if self.sqlalchemy_use_enum:
return sa.Column(
name,
self._sqlalchemy_enum_type(dialect, column_name=name),
nullable=self.nullable,
primary_key=self.primary_key,
unique=self.unique,
autoincrement=False,
)
return super().sqlalchemy_column(name, dialect)
Comment on lines +135 to +139
name = self.sqlalchemy_enum_name
if self._enum_class is not None:
if name is not None:
kwargs["name"] = name
return sa.Enum(self._enum_class, **kwargs)
Comment thread docs/guides/features/sql-generation.md Outdated
By default, {class}`~dataframely.Enum` maps to fixed-length `CHAR` or `VARCHAR` columns so stored values remain plain strings. For PostgreSQL setups that use database-level `ENUM` types (for example with Alembic autogenerate), set `sqlalchemy_use_enum=True`:

```python
from enum import StrEnum
Comment thread docs/guides/features/sql-generation.md Outdated
Comment on lines +94 to +96
class Status(StrEnum):
PENDING = "pending"
APPROVED = "approved"
Comment on lines +178 to +185
class _Status(str, Enum):
PENDING = "pending"
APPROVED = "approved"


@pytest.mark.parametrize(
("column", "dialect", "datatype"),
[
PGDialect_psycopg2(),
"my_status",
),
(dy.Enum(_Status, sqlalchemy_use_enum=True), PGDialect_psycopg2(), "_status"),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Optional native SQLAlchemy Enum for dy.Enum in to_sqlalchemy_columns (Postgres)

2 participants