djangoforeign-keysmanytomanyfieldsyncdb

Django syncdb conflicting related_name when using inheritance and ForeignKey


This time I think it's not me being stupid but an actual conflict. I have the below code (simplified):

from django.db import models

class Alpha(models.Model):
    relation = models.ForeignKey('Delta', related_name = 'reverse_relation', blank = True, null = True)

    class Meta:
        abstract = True

class Beta(Alpha):
    pass

class Gamma(Alpha):
    pass

class Delta(models.Model):
    pass

The problem is that Delta.reverse_relation could refer to an instance of Beta or an instance of Gamma. I would somehow have to provide multiple related_name values (or one that depends on the class name).I think the problem is clear but to be complete, the error (when running syncdb): app.beta: Accessor for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.
app.beta: Reverse query name for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.
app.gamma: Accessor for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.
app.gamma: Reverse query name for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.

Is it possible at all to place the ForeignKey in the parent Alpha, or is the only way to cut-paste this code to Beta and Gamma? I prefer not to do that because it kind of defeats the point of inheritance if I can't define in the parent half the fields that all children share.

Any help is much apprectiated!

(If anyone can comment with why the error messages aren't in a code box I'll fix that.)


Solution

  • I think that you will find the following advice in the Django documentation helpful and relevant: https://docs.djangoproject.com/en/1.7/topics/db/models/#be-careful-with-related-name

    Essentially change the declaration of the relation field to:

    relation = models.ForeignKey('Delta', related_name="%(app_label)s_%(class)s")
    

    Best of luck...