Opened 11 years ago

Closed 11 years ago

#12236 closed (invalid)

When subquery returns more than 1 row MySQL leads to error

Reported by: Ilya Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Code something like: MyModel.objects.filter(key_to_mymodel2 = MyModel2.objects.all()))
produce SQL query:

SELECT * FROM `test_mymodel` WHERE key_to_mymodel2_id =(select `id `from `test_mymodel2`)

It works fine on SQLLite, but in MySQL it cause an error OperationalError: (1242, 'Subquery returns more than 1 row')

For correct work of subquerys on MySQL need be added ANY ([ mysql errors]):

SELECT * FROM `test_mymodel` WHERE key_to_mymodel2_id = ANY (select
`id` from `test_mymodel2`)

Change History (2)

comment:1 Changed 11 years ago by Alex Gaynor

As far as I can tell you are attempting to do an in query, and MyModel.objects.filter(key_to_mymodel2__in = MyModel2.objects.all())) will do the right thing.

comment:2 Changed 11 years ago by Russell Keith-Magee

Resolution: invalid
Status: newclosed

As alex says, the query doesn't make any sense, and the error message tells you why.

Note: See TracTickets for help on using tickets.
Back to Top