I have PostgreSQL 10.5 database with Rails 5 application.
My model:
# == Schema Information
#
# Table name: property_keys
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# i18n_name :jsonb
#
class PropertyKey < ApplicationRecord
# Fields
store :i18n_name, accessors: I18n.available_locales, coder: JSON
end
My migration:
class LocalizeProperties < ActiveRecord::Migration[5.2]
def up
add_column :property_keys, :i18n_name, :jsonb
PropertyKey.all.each do |property_key|
[:en, :de, :ru].each do |locale_key|
property_key.i18n_name[locale_key] = property_key.name
end
property_key.save!
end
end
def down
remove_column :property_keys, :i18n_name
end
end
Table name: property_keys
. List of fields:
- id: bigint
- i18n_name: jsonb
Here is a request for all data:
I want to get all english names (values of "en"
key inside i18n_name
column).
Here is a request:
SELECT
id,
i18n_name,
i18n_name->'en' AS en
FROM property_keys;
And it returns nothing.
But theoretically should return the data with filled "en" column.
Here is a screenshot:
Also I tried query with ->>
and it did not work:
How should I change my request and which PostgreSQL operators should I use to make it works?
Checked length of JSONB columns:
->>
operator? – Darryldarrynwith t(x) as (values('{"en":"a","de":"b","ru":"c"}'::jsonb)) select x, x->'en' from t;
If it will work for you (it should) then try to find the difference. – Antoineantoinettawith t(x) as (values('{"en":"a","de":"b","ru":"c"}'::jsonb)) select x, x->'en' from t;
works! – Vc->
and->>
does not work?... According to Postgres Documentation it should.. – Vcselect '{"en":"asdasdadad","de":"asdasdadad","ru":"asdasdadad"}'::jsonb->'en'
If it works, your data contains some additional characters which you don't see. – Rev"en"
that you don't see. Try this:select length(i18n_name::text), length('{"en":"asdasdadad","de":"asdasdadad","ru":"asdasdadad"}')
and see if it brings up the same number – Reven
key if there is no such key. There lies your answer. Investigate the data to see what's inside or simply fix it by running an UPDATE – Rev{"q": "some text"}
. as "q" - is not cyrryllic character. And->
magic begin to work) – Vc