gpt4 book ai didi

mysql - 无法存储新行 - SQLSTATE [23000] : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

转载 作者:行者123 更新时间:2023-11-30 22:39:58 31 4
gpt4 key购买 nike

我的客户表和用户表是我的业务表的子表。因此 clients 和 users 表都包含一个 business_id 列,该列引用 businesses 表的 id 列:

public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->integer('business_id')->unsigned();
$table->foreign('business_id')->references('id')->on('businesses');
$table->string('first_name');
$table->string('last_name');
Etc…

我能够存储一个新用户,它工作正常,但是当我试图将一个新行存储到我的客户表中时,我总是收到这个错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (laravel.clients, CONSTRAINT clients_business_id_foreign FOREIGN KEY (business_id)

Client.php模型

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['business_id', 'first_name', 'last_name'];

/**
* Business where this user works.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function businesses()
{
return $this->belongsTo('App\Business');
}

Business.php 模型

/**
* Clients that use this business.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function clients()
{
return $this->hasMany('App\Client','business_id');
}

客户端 Controller .php

public function store(ClientsRequest $request)
{
var_dump(Auth::user()->business_id);

$clientinfo = Request::all();

Client::create($clientinfo);

$client = new Client;
$client->business_id = Auth::user()->business_id;
$client->first_name = $clientinfo['first_name'];
$client->last_name = $clientinfo['last_name'];

//$client->save();
$business->clients()->save($client);

$last_inserted_id = $data->id;

return redirect('clients/'.$last_inserted_id.'/edit');

我唯一不确定的是我通过 Auth 检索 business_id(从用户表)的方式。当我 var_dump 该值时,我得到:

string(1) "7"

我知道我的 businesses 表中的 id 值是 7,这就是我要查找的值,但不确定是否应将其转换为整数。

而且,不确定我是否必须使用 :

来保存
 $client->save();

或者

 $business->clients()->save($client);

谢谢

最佳答案

下面两种都是正确的方法,但是第一种比后面的更像Laravel,

public function store(ClientsRequest $request)
{
$client = new Client([
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
]);

$client = Business::findOrFail(Auth::user()->business_id)
->clients()
->save($client);

return redirect('clients/'.$client->id.'/edit');
}

附言上面的 business_id 会被 Eloquent

自动添加到 client

其他方式与您所做的一样,您手动插入 business_id

public function store(ClientsRequest $request)
{
$clientinfo = Request::all();

$client = Client::create([
'business_id' => Auth::user()->business_id;
'first_name' => $clientinfo['first_name'];
'last_name' => $clientinfo['last_name'];
]);

return redirect('clients/'.$client->id.'/edit');
}

由于代码中的以下语句,您收到错误消息,

Client::create($clientinfo);

上面的语句将尝试创建一个没有 business_id 的数据库条目,它是一个外键,应该提供但没有提供,因此出错。

而且您不必担心 int<->string 之间的类型转换,PHP 非常擅长。

Read More

关于mysql - 无法存储新行 - SQLSTATE [23000] : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31392405/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com