- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经建立了以下 Eloquent 模型和关系:
申请人
class Applicant {
public function application()
{
return $this->hasOne(Application::class);
}
}
申请
class Application {
public function applicant()
{
return $this->belongsTo(Applicant::class);
}
protected $fillable = [
// ... other fields
'applicant_id',
];
}
以及他们的迁移:
申请人
表
public function up()
{
Schema::create('applicants', function (Blueprint $table) {
$table->increments('id');
// other fields ...
});
}
应用程序
表
public function up()
{
Schema::create('applications', function (Blueprint $table) {
$table->increments('id');
// other fields ...
$table->integer('applicant_id')->unsigned();
// other relations ...
$table->foreign('applicant_id')->references('id')->on('applications')->onDelete('cascade');
});
}
我只想创建一个新申请人,然后为刚刚创建的申请人添加一个新申请:
$applicant = Applicant::create([
// data
]);
$application = $applicant->applications()->create([
// data
]);
但是,当运行此代码时,我收到以下错误:
SQLSTATE[23000]: Integrity constraint violation:
1452 Cannot add or update a child row: a foreign key constraint fails (
`dbname`.`applications`, CONSTRAINT `applications_applicant_id_foreign`
FOREIGN KEY (`applicant_id`) REFERENCES `applications` (`id`) ON DELETE CASCADE
)
(SQL: insert into `applications` (
`status`,
`reference_number`,
`organisation_id`,
`qualification_id`,
`applicant_id`,
`updated_at`,
`created_at`
) values (
pending,
573066CE59BFE,
24,
1,
12, // <------ applicant_id, i.e. the foreign key!
2016-05-09 11:30:38,
2016-05-09 11:30:38
))
据我了解,发生错误是因为我向其传递了数据库中不存在的外键。所以我想知道我第一次调用数据库来创建申请人时发生了什么:
$applicant = Applicant::create([
// data
]);
在创建应用程序的第二条语句之前是否没有写入数据库:
$application = $applicant->application()->create([
// data
]);
...从而导致完整性约束违规?因为如果我在创建后立即 dd($applicant)
或 dd(Applicant::find($applicant->id))
我会获得记录,并且还可以查看它在数据库中。
我发现在重构代码之前,我调用 create()
来添加新记录,然后逐行构建新的应用程序并使用 save( )
,我没有遇到这个问题。所以我很想知道这两个调用有何不同(如果有的话)以及处理这种情况的正确方法是什么,因为我认为这是非常简单的事情,我之前已经做过很多次了。任何建议将不胜感激,谢谢!
编辑 1
更正关系调用:
$applicant->application()->create(...)
不是:
$applicant->applications()->create(...)
编辑2
运行此代码:
$applicant = Applicant::create($this->applicantDataFromRequest($request));
$application = new Application([
'status' => 'pending',
'reference_number' => $request->get('passport_number'),
'organisation_id' => $request->get('organisation'),
'qualification_id' => $request->get('qualification') ?: null,
]);
$applicant->application()->save($application);
dd('done');
并收到相同的错误(已更新页面上的所有错误):
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dbname`.`applications`, CONSTRAINT `applications_applicant_id_foreign` FOREIGN KEY (`applicant_id`) REFERENCES `applications` (`id`) ON DELETE CASCADE) (SQL: insert into `applications` (`status`, `reference_number`, `organisation_id`, `qualification_id`, `applicant_id`, `updated_at`, `created_at`) values (pending, 5730970933C7B, 24, 1, 22, 2016-05-09 14:56:25, 2016-05-09 14:56:25))
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dbname`.`applications`, CONSTRAINT `applications_applicant_id_foreign` FOREIGN KEY (`applicant_id`) REFERENCES `applications` (`id`) ON DELETE CASCADE)
编辑3添加了相关迁移
更新1
真奇怪!我已经通过 git 恢复到了该项目的旧版本,在我开始重构代码之前它可以正常工作,但现在由于同样的原因它不起作用了!不知道这里发生了什么!?
更新2
刚刚注意到迁移中有些非常愚蠢的事情:
应用程序
表
public function up()
{
Schema::create('applications', function (Blueprint $table) {
$table->increments('id');
// other fields ...
$table->integer('applicant_id')->unsigned();
// other relations ...
$table->foreign('applicant_id')->references('id')->on('applications')->onDelete('cascade');
});
}
外键应该是:
$table->foreign('applicant_id')
->references('id')
->on('applicants')
->onDelete('cascade'); // not applications!
这里有两点:
为什么到现在为止这还没有影响我的代码?一直工作正常,直到现在有点奇怪......
将更新外键约束并尝试一下,看看现在是否有效
最佳答案
试试这个:
$applicant = new Applicant([
// data
]);
$application = new Application([
// data
]);
$applicant->applications()->save($application);
关于php - Laravel Eloquent 创建方法导致完整性约束违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37114055/
我想要一些概念上的澄清。为了证明问题是 NP 完全的,我们使用归约。 现在假设我有 L<=L'。是从 L 减少到 L' 还是我也可以用相反的方式来减少?即我能否证明如果 L 可以使用 L' 求解,那么
考虑不相交的哈密顿路径问题: 输入:一个可能是有向或无向的图 输出:此图是否至少存在 2 条边不相交的哈密顿路径?边不相交意味着没有一条边被两条路径共享。 证明不相交哈密顿路径是 np-完全的。 有人
我理解为什么有界度生成树被认为是度数为 2 的 NP 完全(这是哈密顿路径问题的一个实例),但我不明白为什么这适用于度数 > 2。如果有人可以解释一下为什么这是大于 2 的 NP 完全问题,这将是最有
我正在实现一个 Django 网站,其中上传的文件在保存到服务器 (/media) 之前使用用户提供的 key 进行加密。当用户希望查看它们时,系统会提示他们输入 key ,加密文件被解密,然后显示给
我想用nodejs列出指定目录中的所有文件。 var fs = require('fs'); var path = require('path'); var walk = function(direc
在我的文件夹 assets/data 中,有很多包含我的应用静态数据的 XML 文件。 对于某人来说,检索 APK、修改其中的一部分并安装到设备上真的很容易。 我想通过检查我的 assets/data
我正在努力将我的备份脚本从 shell 转换为 Python。我的旧脚本的功能之一是通过执行以下操作检查创建的 tarfile 的完整性:gzip -t。 这在 Python 中似乎有点棘手。 似乎唯
我正在尝试将包含带有单独 CSS 和 js 文件的 HTML 脚本的 php 文件导入另一个包含我的页眉和页脚的 php 文件。页眉和页脚来自一个模板,该模板使用非常困惑和令人费解的 CSS,基本上对
使用 Flask,我试图验证 cookie 没有被篡改。现在,如果我更改 cookie 值,它只会抛出一个错误,但我想检查代码 is_valid(session['user_id']) 并重定向/重置
在 PHP(和 MySQL)中,我们有许多技术来确保输入的数据有效且安全。添加斜杠、MySQL 的转义字符串和正则表达式是我们经常使用的一些。 我已经看到此链接,该链接对该主题进行了非常初步的介绍,但
下面的代码使用了不安全的 GeneralizedNewtypeDeriving扩展中断 Data.Set通过插入具有不同 Ord 的不同元素实例: {-# LANGUAGE GeneralizedNe
我刚刚在 NPM 上创建了一个新包(这非常简单),我对如何维护包的完整性感兴趣。任何人都可以发布软件包的新版本吗?或者这仅限于我的用户帐户? 如果任何人都可以发布对包的更改,如何监控他们的修改以确保项
我正在尝试使用 Dapper 和 SQLite 来追踪 C# 项目中的数据库损坏错误。所以我正在寻找一种方法来检查代码中的数据库完整性。我发现多个地方说我可以为此发送命令“PRAGMAintegrit
yarn 安装抛出: EACCES: permission denied, unlink '/home/minnak/Darbas/market/node_modules/.yarn-integrit
上下文: 我有 open-sourced a repository ,由 Travis-CI 测试。特拉维斯提供 build-notification用于测试运行的钩子(Hook),因此您可以在 IR
我是一名优秀的程序员,十分优秀!