Code

Opened 4 years ago

Closed 4 years ago

#12236 closed (invalid)

When subquery returns more than 1 row MySQL leads to error

Reported by: ilblackdragon 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: UI/UX:

Description

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`)

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 4 years ago by russellm

  • Resolution set to invalid
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.