Django ForeignKey 值没有对应的值
Django ForeignKey value does not have corresponding value
我正在使用模式 'ForeignKey' 部分处理视频中 30 分钟左右的 CS50 的 Django 部分。
当我 运行 进行迁移时出现错误。
您正试图在没有默认值的情况下向产品添加不可为空的字段 'agent';我们不能这样做(数据库需要一些东西来填充现有行)。
请select修复:
1) 现在提供一次性默认值(将在所有现有行上设置此列的空值)
2) 退出,让我在models.py
中添加一个默认值
Select一个选项:2
如果我将默认值设置为字符串值,我会收到错误消息,提示它需要一个 ID。
如果我将其设置为 1,则会得到以下结果。
table 'clutter_product' 中主键为“1”的行具有无效的外键:clutter_product.agent_id 包含值“1”,该值在 clutter_supplier.id.
class Supplier(models.Model):
company = models.CharField(max_length=64)
contact = models.CharField(max_length=64)
email = models.CharField(max_length=128, blank = True)
def __str__(self):
return f"{self.id} {self.company} {self.contact} {self.email}"
class Product(models.Model):
name = models.CharField(max_length=64)
sku = models.CharField(max_length=64, blank = True)
unit_cost = models.IntegerField()
rrp = models.IntegerField()
average_fee = models.IntegerField()
shipping_fee = models.IntegerField()
agent = models.ForeignKey(Supplier, default=1, on_delete=models.CASCADE, related_name="suppliers")
我建议尝试以下步骤:
首先,删除导致此问题的迁移文件。
其次改模型如下:
class Product(models.Model):
# rest of the fields
agent = models.ForeignKey(Supplier, null=True, default=None, on_delete=models.CASCADE, related_name="suppliers")
第三次 运行 makemigrations and migrate.
第四使用supplier = Supplier.objects.create(company="ABC", contact="contact", email="a@b.c", pk=1)
创建供应商实例
第五(可选)更新现有产品以拥有供应商-Product.objects.update(agent=supplier)
.
Sixth,如果您想限制使用代理创建产品,则从 Product
中的 agent
字段中删除 null=True, default=None
模型。然后 运行 makemigrations 并迁移。
我正在使用模式 'ForeignKey' 部分处理视频中 30 分钟左右的 CS50 的 Django 部分。
当我 运行 进行迁移时出现错误。
您正试图在没有默认值的情况下向产品添加不可为空的字段 'agent';我们不能这样做(数据库需要一些东西来填充现有行)。
请select修复:
1) 现在提供一次性默认值(将在所有现有行上设置此列的空值)
2) 退出,让我在models.py
中添加一个默认值Select一个选项:2
如果我将默认值设置为字符串值,我会收到错误消息,提示它需要一个 ID。
如果我将其设置为 1,则会得到以下结果。
table 'clutter_product' 中主键为“1”的行具有无效的外键:clutter_product.agent_id 包含值“1”,该值在 clutter_supplier.id.
class Supplier(models.Model):
company = models.CharField(max_length=64)
contact = models.CharField(max_length=64)
email = models.CharField(max_length=128, blank = True)
def __str__(self):
return f"{self.id} {self.company} {self.contact} {self.email}"
class Product(models.Model):
name = models.CharField(max_length=64)
sku = models.CharField(max_length=64, blank = True)
unit_cost = models.IntegerField()
rrp = models.IntegerField()
average_fee = models.IntegerField()
shipping_fee = models.IntegerField()
agent = models.ForeignKey(Supplier, default=1, on_delete=models.CASCADE, related_name="suppliers")
我建议尝试以下步骤:
首先,删除导致此问题的迁移文件。
其次改模型如下:
class Product(models.Model):
# rest of the fields
agent = models.ForeignKey(Supplier, null=True, default=None, on_delete=models.CASCADE, related_name="suppliers")
第三次 运行 makemigrations and migrate.
第四使用supplier = Supplier.objects.create(company="ABC", contact="contact", email="a@b.c", pk=1)
第五(可选)更新现有产品以拥有供应商-Product.objects.update(agent=supplier)
.
Sixth,如果您想限制使用代理创建产品,则从 Product
中的 agent
字段中删除 null=True, default=None
模型。然后 运行 makemigrations 并迁移。