Skip to content

Commit cd20c50

Browse files
authored
Fixed enum column rendering on non-default schema (#368)
Fixes #510.
1 parent 13d3701 commit cd20c50

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Version history
44
**UNRELEASED**
55

66
- Dropped support for Python 3.8
7+
- Fixed two rendering issues in ``ENUM`` columns when a non-default schema is used: an
8+
unwarranted positional argument and missing the ``schema`` argument
79

810
**3.0.0rc5**
911

src/sqlacodegen/generators.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ def render_column_type(self, coltype: object) -> str:
512512
if param.name.startswith("_"):
513513
continue
514514
elif param.kind in (Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD):
515+
use_kwargs = True
515516
continue
516517

517518
value = getattr(coltype, param.name, missing)
@@ -535,8 +536,11 @@ def render_column_type(self, coltype: object) -> str:
535536
varargs_repr = [repr(arg) for arg in getattr(coltype, vararg)]
536537
args.extend(varargs_repr)
537538

538-
if isinstance(coltype, Enum) and coltype.name is not None:
539-
kwargs["name"] = repr(coltype.name)
539+
# These arguments cannot be autodetected from the Enum initializer
540+
if isinstance(coltype, Enum):
541+
for colname in "name", "schema":
542+
if (value := getattr(coltype, colname)) is not None:
543+
kwargs[colname] = repr(value)
540544

541545
if isinstance(coltype, JSONB):
542546
# Remove astext_type if it's the default
@@ -683,6 +687,8 @@ def get_adapted_type(self, coltype: Any) -> Any:
683687
kw = {}
684688
if supercls is Enum:
685689
kw["name"] = coltype.name
690+
if coltype.schema:
691+
kw["schema"] = coltype.schema
686692

687693
try:
688694
new_coltype = coltype.adapt(supercls)

tests/test_generator_tables.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ def test_fancy_coltypes(generator: CodeGenerator) -> None:
3939
Table(
4040
"simple_items",
4141
generator.metadata,
42-
Column("enum", postgresql.ENUM("A", "B", name="blah")),
42+
Column("enum", postgresql.ENUM("A", "B", name="blah", schema="someschema")),
4343
Column("bool", postgresql.BOOLEAN),
4444
Column("number", NUMERIC(10, asdecimal=False)),
45+
schema="someschema",
4546
)
4647

4748
validate_code(
@@ -54,9 +55,10 @@ def test_fancy_coltypes(generator: CodeGenerator) -> None:
5455
5556
t_simple_items = Table(
5657
'simple_items', metadata,
57-
Column('enum', Enum('A', 'B', name='blah')),
58+
Column('enum', Enum('A', 'B', name='blah', schema='someschema')),
5859
Column('bool', Boolean),
59-
Column('number', Numeric(10, asdecimal=False))
60+
Column('number', Numeric(10, asdecimal=False)),
61+
schema='someschema'
6062
)
6163
""",
6264
)

0 commit comments

Comments
 (0)