我在cmd中得到django.db.utils.IntegrityError。在cmd中,FOREIGN KEY约束失败。

我从网上复制了一个json文件,我正在用它做django项目。我在django项目中使用了这个文件,在cmd中输入了以下几行—-。

>>> import json
>>> from blog.models import Post
>>> with open('posts.json') as f:
...   posts_json=json.load(f)
...
>>> for post in posts_json:
...   post = Post(title=post['title'], content=post['content'], author_id=post['
user_id'])
...   post.save()
...

而我得到的错误是-

Traceback (most recent call last):
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: FOREIGN KEY constraint failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 3, in <module>
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 745, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 782, in save_base
    updated = self._save_table(
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 887, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields
, raw)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 924, in _do_insert
    return manager._insert(
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\query.py", line 1204, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\sql\compiler.py", line 1391, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._e
xecute)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: FOREIGN KEY constraint failed

这是我的models.py文件 –

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse

class Post(models.Model):
    title=models.CharField(max_length=100)
    content=models.TextField()
    date_posted=models.DateTimeField(default=timezone.now)
    author=models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail',kwargs={'pk':self.pk})

这是我从网上复制的json文件。https:/raw.githubusercontent.comCoreyMSchafercode_snippetsmasterDjango_Blogsnippetsposts.json。

解决方案:

这里唯一的外键是 author,所以外键约束失败一定意味着没有一个id对应于 user_id 字段中的一条记录。

因为一眼看去,好像只有用户id1和2,请确认你的数据库里有这两个用户,然后再试试。

给TA打赏
共{{data.count}}人
人已打赏
解决方案

GET http:/0.0.0.0:8080index.html?age=8&rel= 404 (Object Not Found)

2022-5-11 15:00:38

解决方案

python 将 3d 数组从 MxNxN 变成 NxNxM

2022-5-11 16:00:09

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索