SQLSTATE[23000]:违反完整性约束:1452 无法添加或更新子行:Laravel 4 中的外键约束失败错误
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails error in Laravel 4
我收到以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`database`.`messages`, CONSTRAINT `messages_sender_id_foreign` FOREIGN KEY (`sender_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE) (SQL: insert into `messages` (`subject`, `message`, `draft`, `updated_at`, `created_at`) values (test, testgdsgds, 0, 2015-05-24 16:03:59, 2015-05-24 16:03:59))
所以从这里我可以看出它与我在消息 table 中的 sender_id 字段有关。这是迁移:
public function up()
{
Schema::create('messages', function(Blueprint $table) {
$table->increments('id');
$table->mediumText('subject');
$table->text('message');
$table->boolean('draft');
$table->integer('sender_id')->unsigned();;
$table->softDeletes();
$table->timestamps();
$table->foreign('sender_id')->references('id')->on('users')->onUpdate('cascade');
});
}
这里是我保存信息的地方:
ApiController.php
public function store()
{
$userId = Sentry::getUser()->id;
$input = Input::all();
//Create Message
Message::create([
'subject' => $input['subject'],
'message' => $input['message'],
'draft' => 0,
'sender_id' => $userId
]);
}
EmailRepositoryEloquent.php
public function create(array $data)
{
$model = $this->model->fill($data);
if ($model->save()) return $model;
return false;
}
我可以确认数据在所有方法之间正确传递,所以我目前是用户一,在传递给 create()
方法的 $data
数组中 sender_id
是1,这个ID肯定有用户,我也登录了。
有什么想法吗?
"A FOREIGN KEY in one table points to a PRIMARY KEY in another table."
如果您要插入的值不存在于父项中table,则会导致外键约束失败。
参考请查看https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html。
感谢 Larachat slack 频道的 dondraper_,我发现最近将列名称从 user_id 更新为 sender_id 时,我忘记更改 $fillable 值。一旦 $fillable 被更新为 'sender_id' ,一切都成功了!
我收到以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`database`.`messages`, CONSTRAINT `messages_sender_id_foreign` FOREIGN KEY (`sender_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE) (SQL: insert into `messages` (`subject`, `message`, `draft`, `updated_at`, `created_at`) values (test, testgdsgds, 0, 2015-05-24 16:03:59, 2015-05-24 16:03:59))
所以从这里我可以看出它与我在消息 table 中的 sender_id 字段有关。这是迁移:
public function up()
{
Schema::create('messages', function(Blueprint $table) {
$table->increments('id');
$table->mediumText('subject');
$table->text('message');
$table->boolean('draft');
$table->integer('sender_id')->unsigned();;
$table->softDeletes();
$table->timestamps();
$table->foreign('sender_id')->references('id')->on('users')->onUpdate('cascade');
});
}
这里是我保存信息的地方:
ApiController.php
public function store()
{
$userId = Sentry::getUser()->id;
$input = Input::all();
//Create Message
Message::create([
'subject' => $input['subject'],
'message' => $input['message'],
'draft' => 0,
'sender_id' => $userId
]);
}
EmailRepositoryEloquent.php
public function create(array $data)
{
$model = $this->model->fill($data);
if ($model->save()) return $model;
return false;
}
我可以确认数据在所有方法之间正确传递,所以我目前是用户一,在传递给 create()
方法的 $data
数组中 sender_id
是1,这个ID肯定有用户,我也登录了。
有什么想法吗?
"A FOREIGN KEY in one table points to a PRIMARY KEY in another table."
如果您要插入的值不存在于父项中table,则会导致外键约束失败。
参考请查看https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html。
感谢 Larachat slack 频道的 dondraper_,我发现最近将列名称从 user_id 更新为 sender_id 时,我忘记更改 $fillable 值。一旦 $fillable 被更新为 'sender_id' ,一切都成功了!