﻿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
34614	Natural keys for nullable foreign keys	Martin Beroiz	nobody	"I found that if a fixture has a model with a nullable foreign key (FK) to a model with natural keys, loaddata fails to load the first object.
For example, slightly modifying the docs example:


{{{
class PersonManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name=name)

class Person(models.Model):
    name = models.CharField(max_length=100, unique=True)
    objects = PersonManager()

    def natural_key(self):
            return (self.name,)

class Book(models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(Person, on_delete=models.CASCADE, null=True)
}}}

(notice the null=True addition to Book)

Then this fixture will fail when attempting to load with loaddata

{{{
[
  {
    ""model"": ""myapp.book"",
    ""fields"": {
      ""name"": ""I Ching"",
      ""author"": null,
    }
  },
]
}}}

Notice that ""author"": [null] in the fixture does not fix it either, since it will pass None to the manager's get_by_natural_key. Returning None from get_by_natural_key doesn't help either.

If this is a feature, and models with natural keys can't ever be referenced by other models with a _nullable_ FK to them, I think it should probably be explicit in the documentation.

"	Bug	closed	Core (Serialization)	4.2	Normal	worksforme			Unreviewed	0	0	0	0	0	0
