Django ORM: Case When not working when output is a Subquery producing a list of Ids?

20 views Asked by At

I've been spending an incredible amount of time today on something I don't understand at all. Let me explain:

I have a code similar to this one:

puzzle_sequence__puzzles = <a subquery>
puzzle_sequence__puzzles_3d = <another subquery>
MyModel.objects.add_only_3d_puzzle_sequence().exclude(puzzle__in=Case(
                                When(Q(only_3d_puzzle_sequence=True), 
                                    then=Subquery(puzzle_sequence__puzzles_3d)),
                                default=Subquery(puzzle_sequence__puzzles)
                                ))

which throws an error

sqlite3.OperationalError: near "CASE": syntax error
django.db.utils.OperationalError: near "CASE": syntax error

or

django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE WHEN CASE WHEN (`games_play`.`puzzle_sequence_id` IS NOT NULL AND CASE W...' at line 1")

depending if I'm on sqlite3 or MariaDB server

I've been toying with it for some while now, and it seems that's it's really the Case When statement which raised the issue as both the following statements work well:

MyModel.objects.add_only_3d_puzzle_sequence().exclude(puzzle__in=Subquery(puzzle_sequence__puzzles_3d))
MyModel.objects.add_only_3d_puzzle_sequence().exclude(puzzle__in=Subquery(puzzle_sequence__puzzles))

I however don't understand at all why my Case When statement would fail

Is there a specific issue if the output_field of a Case When statement should be a list of objects or list of object ids ?

PS: as you can see, I'm not using Postgres, so I guess I cannot force output_field=ArrayAgg Thanks in advance

0

There are 0 answers