Python 3.x 在django中使用default和foreignkey模型
我正在做一个django项目,我有3个模型 -类别 -子类别 -产品 子类别是类别的外键,而产品是类别和产品的外键 运行迁移时,会出现以下错误:Python 3.x 在django中使用default和foreignkey模型,python-3.x,django,django-models,Python 3.x,Django,Django Models,我正在做一个django项目,我有3个模型 -类别 -子类别 -产品 子类别是类别的外键,而产品是类别和产品的外键 运行迁移时,会出现以下错误: django.db.utils.IntegrityError:主键为“5”的表“shop\u product”中的行具有无效外键:shop\u product.subcategory\u id包含的值“1”在shop\u subcategory.id中没有相应的值。 型号.py from django.db import models from dja
django.db.utils.IntegrityError:主键为“5”的表“shop\u product”中的行具有无效外键:shop\u product.subcategory\u id包含的值“1”在shop\u subcategory.id中没有相应的值。
型号.py
from django.db import models
from django.urls import reverse
from ckeditor.fields import RichTextField
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = "categories"
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category', args=[self.slug])
class Subcategory(models.Model):
category = models.ForeignKey(Category, related_name='subcategories', on_delete=models.CASCADE)
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
class Meta:
ordering = ('name',)
verbose_name = 'subcategory'
verbose_name_plural = 'subcategories'
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
description = RichTextField(blank=True, null=True)
subcategory = models.ForeignKey(Subcategory, related_name='product', on_delete=models.CASCADE, null=True, default=1)
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail', args=[self.id, self.slug])
终端输出
python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, coupons, orders, sessions, shop
Running migrations:
Applying shop.0002_auto_20201103_1603...Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 229, in apply_migration
migration_recorded = True
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/db/backends/sqlite3/schema.py", line 35, in __exit__
self.connection.check_constraints()
File "/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 343, in check_constraints
raise IntegrityError(
django.db.utils.IntegrityError: The row in table 'shop_product' with primary key '5' has an invalid foreign key: shop_product.subcategory_id contains a value '1' that does not have a corresponding value in shop_subcategory.id.
python3 manage.py迁移
要执行的操作:
应用所有迁移:管理、身份验证、内容类型、优惠券、订单、会话、商店
运行迁移:
正在应用shop.0002_auto_20201103_1603…回溯(最近一次呼叫最后一次):
文件“manage.py”,第22行,在
main()
文件“manage.py”,第18行,主
从命令行(sys.argv)执行命令
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/core/management/__init__;.py”,第401行,从命令行执行
utility.execute()
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/core/management/_init__.py”,第395行,在execute中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site-packages/django/core/management/base.py”,第330行,运行于
self.execute(*args,**cmd_选项)
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/core/management/base.py”,第371行,在execute中
输出=self.handle(*args,**选项)
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/core/management/base.py”,第85行,包装
res=句柄函数(*args,**kwargs)
handle中的文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/core/management/commands/migrate.py”,第243行
post\u migrate\u state=executor.migrate(
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/db/migrations/executor.py”,第117行,在migrate中
状态=self.\u迁移\u所有\u转发(状态,计划,完整计划,假=假,假首字母=假首字母)
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/db/migrations/executor.py”,第147行,全部向前迁移
state=self.apply\u迁移(state,migration,false=false,false\u initial=false\u initial)
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/db/migrations/executor.py”,第229行,在apply_migration中
迁移记录=真
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/db/backends/sqlite3/schema.py”,第35行,在退出时__
self.connection.check_约束()
文件“/home/kolawole/Desktop/myshop/env/lib/python3.8/site packages/django/db/backends/sqlite3/base.py”,第343行,检查约束
提高积分误差(
django.db.utils.IntegrityError:主键为“5”的表“shop_product”中的行具有无效外键:shop_product.subcategory_id包含的值“1”在shop_subcategory.id中没有相应的值。
我使用python 3.8.5和django 3.1.2
有什么有用的建议吗?谢谢。您已经在Product.subcategory foreignkey options中定义了default=1。 似乎子类别中没有id等于1的条目。 您应该在迁移之前提供一个条目,或者更改id以引用现有子类别,或者删除默认选项