Reputation: 176
From questions like this one, I know that the recommended way to make optional foreign keys in django is to set null=True, blank=True
. That way, the value of the foreign key does not have to be set.
This seems fine to me if the foreign key is frequently used in the model. But if the majority of the foreign keys are null values, then wouldn't this violate first normal form and create a lot of wasted space?
Sure, the topic of null values in databases may be controversial, but I still wonder how I should set up optional foreign keys in Django if I know that the foreign key will be sparse. Does Django already take this into account? Should I create a separate model for this relationship? Has this already been taken into account during the design of Django?
Thanks.
Upvotes: 1
Views: 701
Reputation: 59435
You can simulate a nullable foreign key by using a third table:
class City(models.Model):
name = models.CharField(max_length=80)
class Person(models.Model):
name = models.CharField(max_length=80)
class PersonCity(models.Model):
person = models.ForeignKey(Person, unique=True)
city = models.ForeignKey(City)
This way, you will only create a row in the table PersonCity
for those people with a known City
. To access the city of a given person you would use:
city = person.personcity_set().first().city
You can create a custom manager to shorten this syntax and check for a null personcity_set
which I didn't for the sake of example, but I personally think that creating a nullable foreign key is still easier to read and debug.
Upvotes: 1