You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add optional sqlalchemy_use_enum for dy.Enum (#355)
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Andreas Albert <103571926+AndreasAlbertQC@users.noreply.github.com>
Co-authored-by: Andreas Albert <andreas.albert@quantco.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/guides/features/sql-generation.md
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,41 @@ the maximal length of the string is inferred from the regular expression if poss
81
81
maximal lengths can be particularly important for primary key columns. Some database systems, such as Microsoft SQL Server, do not allow `VARCHAR(max)` columns (unbounded strings) to be used as primary keys.
82
82
```
83
83
84
+
## Native SQL enums
85
+
86
+
By default, {class}`~dataframely.Enum` maps to `sa.CHAR` or `sa.String` columns so stored values remain plain strings. You may set `sqlalchemy_use_enum=True` to instead generate native enums:
87
+
88
+
```python
89
+
from enum import Enum, auto
90
+
91
+
import sqlalchemy as sa
92
+
import dataframely as dy
93
+
from sqlalchemy.dialects.postgresql import dialect as pg_dialect
94
+
from sqlalchemy.dialects.mssql import dialect as mssql_dialect
95
+
96
+
97
+
classStatus(str, Enum):
98
+
PENDING= auto()
99
+
APPROVED= auto()
100
+
101
+
102
+
classStaged(dy.Schema):
103
+
status = dy.Enum(Status, sqlalchemy_use_enum=True)
104
+
```
105
+
106
+
This will translate the `~dataframely.Enum` to a `~sqlalchemy.Enum`:
Depending on the database dialect you use, `sqlalchemy` will render this accordingly.
114
+
For example, `postgresql` supports native enums, and `sqlalchemy` will create a native enum column, while in MSSQL, where this is not supported, it will fall back to `VARCHAR`.
115
+
116
+
When `categories` is a Python `enum.Enum` subclass, `sqlalchemy` uses the enum class name (lowercased) as the database enum type name.
117
+
For string category lists, the SQL column name is used by default; override it with `sqlalchemy_enum_name` if needed.
118
+
84
119
## Collections of multiple tables
85
120
86
121
If you have an entire `dy.Collection`, it's also easy to generate one table for each member table of the collection.
0 commit comments