【问题标题】:How to update mulltiple "keys" of a jsonb column in PostgresSQL using Laravel Eloquent如何使用 Laravel Eloquent 更新 PostgresQL 中 jsonb 列的多个“键”
【发布时间】: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 列的值将有多个键,例如:phplaraveldb

{"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


    【解决方案1】:

    要更新jsonb 列中的记录,您可以使用以下命令:

    $values = [
       "options" => [
           "php" => "8.1.0",
           "laravel" => "master",
           "db" => "14",
        ]
    ];
    
    Specs::where('id', 1)->update($values);
    

    【讨论】:

    • 这将从 db.json 中删除保存在 json 中的其他键。我只想更新几个键并保持其他键不变
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2018-07-19
    • 2021-12-07
    • 2015-07-16
    • 2022-01-06
    • 2021-03-30
    • 2021-12-27
    相关资源
    最近更新 更多