pypika icon indicating copy to clipboard operation
pypika copied to clipboard

ComplexCriterion does not use aliases

Open MarkusShepherd opened this issue 3 years ago • 3 comments

It appears that ComplexCriterion ignores an alias attached to it. From the code I can see that other criteria have a signature like def get_sql(self, with_alias: bool = False, ...), but ComplexCriterion lacks that handling of with_alias.

Consequently, when I do something like

.select((criterion_1 | criterion_2).as_("my_alias"))

the SQL version will not include that my_alias.

MarkusShepherd avatar Mar 02 '22 09:03 MarkusShepherd

I was just bitten by this.

randy3k avatar May 06 '22 05:05 randy3k

A workaround solution for now.

from pypika.utils import format_alias_sql

try:
  _original_ComplexCriterion_get_sql
except NameError:
  _original_ComplexCriterion_get_sql = ComplexCriterion.get_sql


def _patched_get_sql(self, *args, **kwargs):
  return format_alias_sql(
      _original_ComplexCriterion_get_sql(self, *args, **kwargs), self.alias, **kwargs)


ComplexCriterion.get_sql = _patched_get_sql

randy3k avatar May 07 '22 01:05 randy3k

see https://github.com/kayak/pypika/pull/735

Ga68 avatar Jul 07 '23 13:07 Ga68