﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27484	ManyToMany relationship filter - delete - generate wrong sql	aangelov	nobody	"Hi, I run Django on legacy database. 

I generated the models.py by inspectdb.

When I run:


{{{
MaterialCode.objects.filter(
             material=material,
             code__type__in=[
                    'industry', 'topic', 'country',
          ]
).delete()
}}}


Djnago generate sql like this:


{{{
DELETE FROM ""material_code""
WHERE ""material_code"".""code_id"" IN
(SELECT U0.""code_id"" AS Col1
  FROM ""material_code"" U0
  INNER JOIN ""code"" U1 ON (U0.""code_id"" = U1.""code_id"")
    WHERE
      (
        U1.""type"" IN ('industry', 'topic', 'country')
        AND U0.""material_id"" = 546437
      )
    )
}}}


which deletes records that isn't supposed to, the verification for material_id is not in the right place.
I can submit the Material, MaterialCode and Code models if it is necessary."	Bug	closed	Database layer (models, ORM)	1.10	Normal	needsinfo	sql generation problem		Unreviewed	0	0	0	0	0	0
