0

I'm having a problem when using multi-table inheritance in Django and I didn't find something that solved it.

I have these two models:

class Person(models.Model):
    id = models.CharField(primary_key=True, max_length=12, default="")
    name = models.CharField(max_length=12, default="")
    birthday = models.DateField()

class Parent(Person):
    work = models.CharField(max_length=70, default="")
    spouce_field = models.OneToOneField(Person, on_delete=DO_NOTHING, related_name="spouce_field")

And I get this error when running python3 manage.py makemigrations:

ERRORS:

family.Parent.spouce_field: (models.E006) The field 'spouce_field' clashes with the field 'spouce_field' from model 'person.person'.

Any idea what am I doing wrong?

davidalk
  • 87
  • 1
  • 1
  • 6
  • You'd probably want `symmetric=True` on the OneToOneField. Also, why is that one named `..._field` when none of the other fields are? :) – AKX Dec 13 '21 at 08:09
  • I think symmetric is only used in many to many, isn't it? When putting it on the OneToOneField I get an error saying `an unexpected keyword argument`. The ..._field is just an attempt of mine to change the actual name of the field, I thought it might fix the problem, it clearly did not :) – davidalk Dec 13 '21 at 09:08
  • Ah, my bad on the `symmetric`. Either way, then you'll need `spouse = models.OneToOneField("Person", related_name="reverse_spouse")` or similar, but it still easily gets pretty weird data-model-wise since you could conceivably have a chain of spouses :) – AKX Dec 13 '21 at 10:02

1 Answers1

1

I believe mrcai have answered your question here:

Mark your class Person as an abstract class to avoid field clashing.

You can specify Profile is an abstract class. This will stop the check from being confused with your parent fields.

class Meta:
   abstract = True
ASG
  • 21
  • 3