PostgreSQL:使用相同名称更新多个内部jsonb对象字段

时间:2019-04-26 06:18:54

标签: postgresql sql-update crud jsonb

我有一个名为import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense,Dropout,Activation,Flatten, Conv2D, MaxPooling2D import pickle import keras as ks x1 =pickle.load(open("dX.pickle","rb")) y2 =pickle.load(open("dY.pickle","rb")) nx = x1/255.0 model = Sequential() model.add(Conv2D(64,(3,3),input_shape = nx.shape[1:])) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(64,3,3)) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size = (2,2))) model.add(Flatten()) model.add(Dense(64)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss = "binary_crossentropy", optimizer ="adam", metrics = ['accuracy']) model.fit((nx,y2), batch_size = 20, validation_split =0.1,epochs=1) img = image.load_img(r'img.png', target_size=(224,224)) prediction = model.prediction(img) print(prediction) 的表,该表有2列:tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'activation_2_target' with dtype float and shape [?,?] [[{{node activation_2_target}}]] test列可以包含这样的数据

(id int, md jsonb)

现在我想将md的所有实例更新为{ "a": { ... "author": "alice" ... }, "b": { ... "author": "alice" ... } }

通过这样做,我得到了包含alice的行的ID

bob

是否有Postgres工具来更新每个包含alice字段的内部对象?

任何建议都值得赞赏。

1 个答案:

答案 0 :(得分:1)

我同意@a_horse_with_no_name,最好查看您的存储空间。但是作为执行程序很有趣。我认为唯一的方法是使用jsonb_each扩展json,使用jsonb_set更新数据,然后使用jsonb_object_agg将数据聚合回去:

update test as t set
    md = (
    select
        jsonb_object_agg(
            d.key,
            case
                when d.value->>'author' = 'alice' then
                    jsonb_set(d.value, '{author}', '"bob"')
                else
                    d.value
            end
        )
     from lateral jsonb_each(t.md) as d
    )
where
    exists (select * from jsonb_each(t.md) as d where d.value->>'author' = 'alice')

db<>fiddle demo