【发布时间】:2021-09-09 13:52:09
【问题描述】:
尝试使用 Laravel Eloquent 更新 PostgresSQL 中 jsonb 列的多个“键”,并引发以下异常
SQLSTATE[42601]: Syntax error: 7 ERROR: multiple assignments to same column "options" (SQL: update "specs" set "options" = jsonb_set("options"::jsonb, '{"php"}', "8.1.0"), "options" = jsonb_set("options"::jsonb, '{"laravel"}', "master"), "options" = jsonb_set("options"::jsonb, '{"db"}', "14"), where "id" = 1)
架构
Schema::create('specs', function (Blueprint $table) {
$table->id();
$table->jsonb('options')->nullable();
});
options 列的值将有多个键,例如:php、laravel、db
{"php": "7.0.8", "laravel": "8.47.0", "db": "postgres 13.3"}
我正在尝试更新以下值,有没有其他方法可以生成“正确”的 SQL 查询
$values = [
"options->php" => "8.1.0",
"options->laravel" => "master",
"options->db" => "14",
];
Specs::where('id', 1)->update($values);
正确的 SQL 查询应该是以下任一
-- Recursive
update "specs"
set "options" = jsonb_set(
jsonb_set(
jsonb_set(
"options" :: jsonb,
'{"db"}',
"14"
),
'{"laravel"}',
"master"
),
'{"php"}',
"8.1.0"
),
where "id" = 1;
-- Merge
update "specs"
set "options" = options || '{"php": "8.1.0", "laravel": "master", "db": "14"}',
where "id" = 1
【问题讨论】:
标签: php laravel postgresql eloquent jsonb