[英]Laravel - Integrity constraint violation: 1452 Cannot add or update a child row
[英]Occasionally Laravel firstOrCreate results in 0 id value and results in Integrity constraint violation: 1452 Cannot add or update a child row
作業文件
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$address = $this->setupCustomer();
} catch (\Exception $exception) {
throw $exception;
}
}
protected function setupCustomer(): CustomerAddress
{
$customer = Customer::firstOrCreate(
[
'email' => $this->transactionDetails['email'],
],
[
'first_name' => $this->transactionDetails['first_name'],
'last_name' => $this->transactionDetails['last_name'],
'phone' => $this->transactionDetails['phone'] ?? '',
]
);
return $customer->addAddress([
'street_address' => $this->transactionDetails['delivery_address']['address'],
'street_address2' => $this->transactionDetails['delivery_address']['address2'],
'postal_code' => $this->transactionDetails['delivery_address']['post_code'],
'city' => $this->transactionDetails['delivery_address']['city'],
'region' => $this->transactionDetails['delivery_address']['region'],
'country' => $this->transactionDetails['delivery_address']['country'],
]);
}
客戶.php Model
public function addAddress(array $address): CustomerAddress
{
$countryId = optional(Country::byCountryCode($address['country'])
->first())
->id;
$address = $this->addresses()->firstOrCreate([
'country_id' => $countryId ?? 0,
'street_address' => $address['street_address'],
'street_address2' => $address['street_address2'] ?? null,
'post_code' => $address['postal_code'],
'city' => $address['city'],
'region' => $address['region'],
]);
return $address;
}
/**
* Customer can have many addresses.
*
* @return HasMany
*/
public function addresses()
{
return $this->hasMany(CustomerAddress::class);
}
有時,我會收到以下錯誤:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`tbps`.`customer_addresses`, CONSTRAINT `customer_addresses_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`)) (SQL: insert into `customer_addresses` (`country_id`, `street_address`, `street_address2`, `post_code`, `city`, `region`, `customer_id`, `updated_at`, `created_at`) values (<country_id>, <street_address>, ?, <post_code>, <city>, ?, 0, <timestamp>, <timestamp>))
為什么在 SQL 語句中以 customer_id = 0 結尾? 客戶總是在數據庫中創建——我無法復制,因為它偶爾會發生。
Object 來自條紋
{
"payment_id": "pi_3MRiqNE7F1cRlWVXXXXXXXXX",
"currency": "gbp",
"payment_type": "card",
"email": "XXXXXXXX@gmail.com",
"name": null,
"first_name": "XXXXX",
"last_name": "XXXX",
"phone": "07XXXXXXXXX",
"purchase_dt": "2023/XX/XX XX:XX:XX",
"discount_code": null,
"delivery_address": {
"address": "<address>",
"address2": null,
"city": "<city>",
"post_code": "<post_code>",
"region": null,
"country": "<country>"
},
...
...
}
我也有這個問題:這就是我們的設置方式:
class ModelOne
{
public function modelTwo()
{
return $this->belongsTo(ModelTwo::class);
}
}
class ModelTwo
{
public function modelOnes()
{
return $this->hasMany(ModelOne::class);
}
}
$modelOne = ModelOne::findOrFail(1);
$modelTwo = ModelTwo::firstOrCreate([
'tenant_id' => 1,
'machine_name' = 'my_new_model_1',
], [
'name' => 'My new model',
]);
$modelOne->modelTwo()->associate($modelTwo);
然后我隨機得到:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db`.`model_one`, CONSTRAINT `model_one_model_two_id_foreign` FOREIGN KEY (`model_two_id`) REFERENCES `model_two` (`id`) ON DELETE SET NULL) (SQL: update `model_one` set `model_two_id` = 0, `model_one`.`updated_at` = ? where `id` = ?)
但我無法在本地復制它,我只是收到客戶要求修復它的電子郵件......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.