Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31442 closed Bug (invalid)

Inherited ManyToMany.all() empty with multiple inherited models of same name from abstract one

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

Description

Please see #24156 first!

The issue described there still persists if the derived model classes have the same class name (spread across different apps).
I understand this to be a weird model design at least, but found it to be reasonable for my current project.

I'd suggest to either use the fully qualified class name (i.e. app.model_class instead of model_class) to build the related_name, or at least note in the documentation that models with identical names can be a problem with inheritance.

Attachments (1)

bug.tgz (4.5 KB ) - added by Gereon Kremer 5 years ago.
Example project showcasing the issue.

Download all attachments as: .zip

Change History (5)

comment:1 by Carlton Gibson, 5 years ago

Hi Gereon — are you able to upload a sample project reproducing this? (I can try to recreate from the description but it's a bit incomplete.)

Seems plausible though...

Last edited 5 years ago by Carlton Gibson (previous) (diff)

by Gereon Kremer, 5 years ago

Attachment: bug.tgz added

Example project showcasing the issue.

comment:2 by Gereon Kremer, 5 years ago

Sure, see the new attachment.
Running ./manage.py makemigrations yields

SystemCheckError: System check identified some issues:

ERRORS:
appA.Stuff.owners: (fields.E305) Reverse query name for 'Stuff.owners' clashes with reverse query name for 'Stuff.owners'.
        HINT: Add or change a related_name argument to the definition for 'Stuff.owners' or 'Stuff.owners'.
appB.Stuff.owners: (fields.E305) Reverse query name for 'Stuff.owners' clashes with reverse query name for 'Stuff.owners'.
        HINT: Add or change a related_name argument to the definition for 'Stuff.owners' or 'Stuff.owners'.

comment:3 by Carlton Gibson, 5 years ago

Resolution: invalid
Status: newclosed

OK, thanks. So the system check error that spells out the problem:

$ ./manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
app1.Child.field1: (fields.E305) Reverse query name for 'Child.field1' clashes with reverse query name for 'Child.field1'.
	HINT: Add or change a related_name argument to the definition for 'Child.field1' or 'Child.field1'.
app2.Child.field1: (fields.E305) Reverse query name for 'Child.field1' clashes with reverse query name for 'Child.field1'.
	HINT: Add or change a related_name argument to the definition for 'Child.field1' or 'Child.field1'.

See the docs section: Be careful with related_name and related_query_name. Specify something like related_name="%(app_label)s_%(class)s_related" on the ManyToManyField on the abstract model.

comment:4 by Gereon Kremer, 5 years ago

Oh sorry, I did not see this particular section...

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