Skip to content

FIX: support IntegerChoices params in raw GROUP BY queries (#540) - #541

Merged
bewithgaurav merged 1 commit into
devfrom
bewithgaurav/fix-integerchoices-groupby-540
Jul 24, 2026
Merged

FIX: support IntegerChoices params in raw GROUP BY queries (#540)#541
bewithgaurav merged 1 commit into
devfrom
bewithgaurav/fix-integerchoices-groupby-540

Conversation

@bewithgaurav

Copy link
Copy Markdown
Collaborator

raw query with a GROUP BY clause plus an IntegerChoices arg raised NotImplementedError:

NotImplementedError: Not supported type <enum 'StatusChoices'> (StatusChoices.IN_PROGRESS)

root cause: any query containing GROUP BY goes through format_group_by_params, which types each param and calls _as_sql_type. that function used exact type checks (typ == int), and type(IntegerChoices_value) is the enum class, not int. even though it subclasses int, enum_cls == int is False, so it fell through to the raise. plain ints and the ORM path never hit this. regression from #354.

fix: switch the int/bool branches to isinstance. bool moves above int because bool is itself an int subclass and must not fall into the int branch.

verified against SQL Server 2022:

  • before: IntegerChoices arg raised NotImplementedError
  • after: IntegerChoices arg returns correct rows, bool still binds BIT, plain int unchanged
  • full testapp suite: 208 passed (3 skipped, 3 expected failures, all pre-existing)

added a regression test (TestIntegerChoicesGroupby) that fails without the fix with the exact NotImplementedError above.

Closes #540

_as_sql_type used exact type checks (typ == int / typ == bool), so an
IntegerChoices value (an int subclass) missed the int branch and hit
NotImplementedError whenever a raw query contained a GROUP BY. switched
both branches to isinstance and moved the bool check above int, since
bool is itself an int subclass.

Issue: #540

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 23, 2026 11:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression in the SQL Server backend’s raw GROUP BY parameter handling so that models.IntegerChoices values (which are int subclasses) can be bound without raising NotImplementedError.

Changes:

  • Update CursorWrapper._as_sql_type() to use isinstance(..., bool) / isinstance(..., int) so IntegerChoices members are treated as integers, with bool handled first.
  • Add a regression test that executes a raw GROUP BY query with an IntegerChoices argument.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
mssql/base.py Adjusts SQL type inference for GROUP BY parameter declaration to accept int subclasses (including IntegerChoices) and keep bool mapping to BIT.
testapp/tests/test_queries.py Adds a regression test ensuring raw GROUP BY queries accept IntegerChoices parameters without error.

Comment thread mssql/base.py
@bewithgaurav
bewithgaurav merged commit 39bc1fa into dev Jul 24, 2026
67 of 68 checks passed
@microsoft microsoft deleted a comment from github-actions Bot Jul 24, 2026
@bewithgaurav bewithgaurav mentioned this pull request Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot directly provide IntegerChoices enums to raw queries that contain a GROUP BY clause

3 participants