【发布时间】:2012-01-13 03:21:24
【问题描述】:
我有一个这样的模型:
from django.db import models
class House(models.Model):
address = models.CharField(max_length=200)
price = models.CharField(max_length=200)
class Inhabitant(models.Model):
home = models.ForeignKey(House, blank=True, null=True, on_delete=models.SET_NULL)
name = models.CharField(max_length=200)
age = models.IntegerField()
但是在运行python manage.py sql my_test_app 时,我最后得到了这个:
ALTER TABLE `my_test_app_inhabitant`
ADD CONSTRAINT `house_id_refs_id_7cd928c7`
FOREIGN KEY (`home_id`)
REFERENCES `my_test_app_house` (`id`);
这意味着我无法添加没有房屋的居民 - 我该如何解决这个问题?
【问题讨论】:
-
你在家里自己定义 - models.ForeignKey(...) :)
标签: python mysql django django-models