On Tue, Nov 17, 2009 at 3:17 PM, Ilya <[email protected]> wrote:
> I just developed some code like:
> MyModel.objects.filter(key_to_mymodel2 = MyModel2.objects.all()))
> This query produce SQL:
> SELECT * FROM `test_mymodel` WHERE key_to_mymodel2_id =(select `id`
> from `test_mymodel2`)
>
> It works fine on SQLLite, but in MySQL it produce error:
> OperationalError: (1242, 'Subquery returns more than 1 row')
> MySQL to work need query:
> SELECT * FROM `test_mymodel` WHERE key_to_mymodel2_id = ANY (select
> `id` from `test_mymodel2`)
>
> Of course I can make something like:
> list = MyModel2.objects.all()
> res = []
> for x in list:
> res += MyModel.objects.filter(key_to_mymodel2 = x)
>
> It there any other path to solve this?
>
> --
>
This would also work:
MyModel.objects.filter(key_to_mymodel2__in=MyModel2.objects.all().values_list('id',
flat=True)))
Cheers
Tom
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=.