Opened 4 years ago

Closed 4 years ago

#27484 closed Bug (needsinfo)

ManyToMany relationship filter - delete - generate wrong sql

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

Description

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.

Change History (1)

comment:1 Changed 4 years ago by Simon Charette

Resolution: needsinfo
Status: newclosed

Hi Aangelov,

Thanks for your report! From what I can see the appropriate SQL is generated -- the material_id constraint is in the right place.

Please re-open this ticket with your model definition (with preferably only the minimum set of fields to reproduce your issue) and the SQL you expect to be generated. A test case demonstrating unexpected deletion would also be great but not mandatory at this point.

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