Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Version history
- Fixed a column named ``registry`` being rendered as-is, which made SQLAlchemy warn
that the attribute should be left reserved for the declarative class registry; it is
now renamed to ``registry_`` the same way a ``metadata`` column already was
- Fixed expression indexes (such as ``lower(email)``) being rendered without their
expressions, which made ``create_all`` fail or silently dropped the expression part
(`#493 <https://github.com/agronholm/sqlacodegen/pull/493>`_; PR by @jayhemnani9910)

**4.0.4**

Expand Down
10 changes: 9 additions & 1 deletion src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def collect_imports_for_constraint(self, constraint: Constraint | Index) -> None
if isinstance(constraint, Index):
if len(constraint.columns) > 1 or not uses_default_name(constraint):
self.add_literal_import("sqlalchemy", "Index")
if any(not isinstance(expr, Column) for expr in constraint.expressions):
self.add_literal_import("sqlalchemy", "text")
elif isinstance(constraint, PrimaryKeyConstraint):
if not uses_default_name(constraint):
self.add_literal_import("sqlalchemy", "PrimaryKeyConstraint")
Expand Down Expand Up @@ -424,7 +426,13 @@ def render_table(self, table: Table) -> str:
return render_callable("Table", *args, kwargs=kwargs, indentation=" ")

def render_index(self, index: Index) -> str:
extra_args = [repr(col.name) for col in index.columns]
# expressions holds the columns and the SQL expressions (lower(email)) in order
extra_args = [
repr(expr.name)
if isinstance(expr, Column)
else render_callable("text", repr(str(expr)))
for expr in index.expressions
]
kwargs = {
key: repr(value) if isinstance(value, str) else value
for key, value in sorted(index.kwargs.items(), key=lambda item: item[0])
Expand Down
29 changes: 29 additions & 0 deletions tests/test_generator_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,35 @@ def test_table_name_identifiers(generator: CodeGenerator) -> None:
)


def test_expression_indexes(generator: CodeGenerator) -> None:
users = Table(
"users",
generator.metadata,
Column("id", INTEGER),
Column("email", VARCHAR),
Index("ix_email_lower", text("lower(email)"), unique=True),
)
users.indexes.add(Index("ix_id_email_lower", users.c.id, text("lower(email)")))

validate_code(
generator.generate(),
"""\
from sqlalchemy import Column, Index, Integer, MetaData, String, Table, text

metadata = MetaData()


t_users = Table(
'users', metadata,
Column('id', Integer),
Column('email', String),
Index('ix_email_lower', text('lower(email)'), unique=True),
Index('ix_id_email_lower', 'id', text('lower(email)'))
)
""",
)


@pytest.mark.parametrize("generator", [["noindexes"]], indirect=True)
def test_option_noindexes(generator: CodeGenerator) -> None:
simple_items = Table(
Expand Down