Laravel 8 upsert 没有按预期工作
Laravel 8 upsert doesn't work as expected
我正在尝试按照此处 () 和 8.x 文档中的说明使用更新插入,但它没有按预期工作。
CartItem::upsert(
[
[
"id" => 105,
"unit_price" => 146.5124,
"price" => 293.0248,
"unit_taxes" => 28.22,
"taxes" => 56.44,
"custom_fields" => '{"options":{"estado":15,"user_id":1},"calculated_price":146.5124,"calculated_tax_price":28.22}'
],
[
"id" => 106,
"unit_price" => 113.8824,
"price" => 227.7648,
"unit_taxes" => 11.54,
"taxes" => 23.08,
"custom_fields" => '{"options":{"estado":15,"user_id":1},"calculated_price":"113.8824","calculated_tax_price":"11.539999999999999"}'
],
],
'id'
);
上面的代码产生了以下错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'cart_id' doesn't have a default value (SQL: insert into cart_items
(created_at
, custom_fields
, id
, price
, taxes
, unit_price
, unit_taxes
, updated_at
) values (2022-01-14 20:41:44, {"options":{"estado":15,"user_id":1},"calculated_price":146.5124,"calculated_tax_price":28.22}, 105, 293.0248, 56.44, 146.5124, 28.22, 2022-01-14 20:41:44), (2022-01-14 20:41:44, {"options":{"estado":15,"user_id":1},"calculated_price":"113.8824","calculated_tax_price":"11.539999999999999"}, 106, 227.7648, 23.08, 113.8824, 11.54, 2022-01-14 20:41:44) on duplicate key update id
= values(id
), unit_price
= values(unit_price
), price
= values(price
), unit_taxes
= values(unit_taxes
), taxes
= values(taxes
), custom_fields
= values(custom_fields
), updated_at
= values(updated_at
))
id
栏是table的PK。我也尝试使用第三个参数但没有成功。
我不明白为什么它没有按预期工作。
由于某种原因,行存在但未检测到。该错误表明 laravel 正在尝试创建新行而不是更新前一行。
是不是少了什么?
相关行:
table定义:
编辑 1:关于列类型,价格和 unit_price 是整数,但我尝试了浮动,因为在 CartItem 中我在 $casts
中配置了它们变量将它们转换为“Money”类型(将它们保存为美分)。我尝试使用 int 值,但得到了相同的结果。
Laravel upserts 执行包含所有行的单个查询,同时使用 upsert,其中包括 MySQL 中的重复键更新和冲突...在 Postgres 中执行更新集。此命令指示数据库更新已存在的记录。
您需要将 cart_id 传递给 MySQL 尝试在重复出现错误之前执行 INSERT。
我正在尝试按照此处 (
CartItem::upsert(
[
[
"id" => 105,
"unit_price" => 146.5124,
"price" => 293.0248,
"unit_taxes" => 28.22,
"taxes" => 56.44,
"custom_fields" => '{"options":{"estado":15,"user_id":1},"calculated_price":146.5124,"calculated_tax_price":28.22}'
],
[
"id" => 106,
"unit_price" => 113.8824,
"price" => 227.7648,
"unit_taxes" => 11.54,
"taxes" => 23.08,
"custom_fields" => '{"options":{"estado":15,"user_id":1},"calculated_price":"113.8824","calculated_tax_price":"11.539999999999999"}'
],
],
'id'
);
上面的代码产生了以下错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'cart_id' doesn't have a default value (SQL: insert into
cart_items
(created_at
,custom_fields
,id
,price
,taxes
,unit_price
,unit_taxes
,updated_at
) values (2022-01-14 20:41:44, {"options":{"estado":15,"user_id":1},"calculated_price":146.5124,"calculated_tax_price":28.22}, 105, 293.0248, 56.44, 146.5124, 28.22, 2022-01-14 20:41:44), (2022-01-14 20:41:44, {"options":{"estado":15,"user_id":1},"calculated_price":"113.8824","calculated_tax_price":"11.539999999999999"}, 106, 227.7648, 23.08, 113.8824, 11.54, 2022-01-14 20:41:44) on duplicate key updateid
= values(id
),unit_price
= values(unit_price
),price
= values(price
),unit_taxes
= values(unit_taxes
),taxes
= values(taxes
),custom_fields
= values(custom_fields
),updated_at
= values(updated_at
))
id
栏是table的PK。我也尝试使用第三个参数但没有成功。
我不明白为什么它没有按预期工作。 由于某种原因,行存在但未检测到。该错误表明 laravel 正在尝试创建新行而不是更新前一行。
是不是少了什么?
相关行:
table定义:
编辑 1:关于列类型,价格和 unit_price 是整数,但我尝试了浮动,因为在 CartItem 中我在 $casts
中配置了它们变量将它们转换为“Money”类型(将它们保存为美分)。我尝试使用 int 值,但得到了相同的结果。
Laravel upserts 执行包含所有行的单个查询,同时使用 upsert,其中包括 MySQL 中的重复键更新和冲突...在 Postgres 中执行更新集。此命令指示数据库更新已存在的记录。 您需要将 cart_id 传递给 MySQL 尝试在重复出现错误之前执行 INSERT。