Skip to content

Autogenerated postgresql.ExcludeConstraint() escaping #478

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by jahs (@jahs)

There appears to be an inconsistency within SQL Alchemy which autogenerated ExcludeConstraint migrations trigger, and an easy workaround. This is only seen for column names that need escaping, such as those containing upper case characters.

I'm using: alembic==0.9.7, SQLAlchemy==1.2.1

Adding an ExcludeConstraint to a Table results in a migration having a line such as:

postgresql.ExcludeConstraint(('"JB_NAM_JB_ID"', '='),
                             ('"JB_NAM_During"', '&&'),
                             using='gist', name='hkJB_NAM_Job_Name')

which appears to be correct following the ExcludeConstraint documentation, namely that the column names should be raw, escaped SQL strings.

However, at sqlalchemy/sql/schema.py(2668)_set_parent() we have

2665 	    def _set_parent(self, table):
2666 	        for col in self._pending_colargs:
2667 	            if isinstance(col, util.string_types):
2668 ->	                col = table.c[col]
2669 	            self.columns.add(col)

and so we get a KeyError as col is assumed not to be escaped.

Following the suggestion in the ExcludeConstraint docs, wrapping the column names in Column() rather than double quotes fixes it:

postgresql.ExcludeConstraint((sa.Column('JB_NAM_JB_ID'), '='),
                             (sa.Column('JB_NAM_During'), '&&'),
                             using='gist', name='hkJB_NAM_Job_Name')

This seems to be a better fix than trying to unescape in SQL Alchemy.

Many thanks, and thank you for Alembic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions