我正在尝试向postgresql jsonb列添加部分索引。 jsonb列的名称为: email_provider 。我已尝试在列上添加部分索引,如下所示,但它会抛出不同的错误,例如 PG :: UndefinedColumn:错误:列“email_povider”不存在,有时它会引发错误: PG :: AmbiguousFunction:错误:运算符不唯一:未知 - >未知
rails-5 迁移中的部分索引如下所示:
add_index :accounts, :name, name: "index_accounts_on_email_provider_kind", using: :gin, where: "('email_provider' -> 'sparkpost' ->> 'smtp_host' = 'www.sparkpost.com') AND ('email_povider' -> 'amazon ses' ->> 'smtp_host' = 'www.awses.com')"
email_provider列的json如下所示:
{
"email_provider": {
"sparkpost": {
"smtp_host": "www.sparkpost.com",
"smtp_port": ""
},
"aws ses": {
"smtp_host": "www.amazon/ses.com ",
"smtp_port": " ",
"username": " ",
"password": " "
}
}
}
表格如下:
class CreateAccounts < ActiveRecord::Migration[5.0]
def change
create_table :accounts do |t|
t.string :name, null: false
t.jsonb :email_provider, null: false
t.jsonb :social_account, default: '[]', null: false
t.timestamps
end
add_index :accounts, :name, name: "index_accounts_on_email_provider_kind", using: :gin, where: "('email_provider' -> 'sparkpost' ->> 'smtp_host' = 'www.sparkpost.com') AND ('email_povider' -> 'amazon ses' ->> 'smtp_host' = 'www.awses.com')"
add_index :accounts, :name, name: "index_accounts_on_social_account_type", using: :gin, where: " 'social_account' @> [{'type': 'facebook'}] AND 'social_account' @> [{'type': 'twitter'}]"
end
end
更新
根据对下面接受的答案的轻微调整,我在rails activerecord中用于创建 btree 而非 gin 索引的代码如下所示。它创建了一个btree索引,因为我们使用的是字符串数据类型的名称列而不是here描述的jsonb:
add_index :accounts, :name, name: "index_accounts_on_name_email_provider", where: "email_provider -> 'sparkpost' ->> 'smtp_host' = 'www.sparkpost.com' AND email_provider -> 'amazon ses' ->> 'smtp_host' = 'www.awses.com' "
add_index :accounts, :name, name: "index_accounts_on_name_social_account", where: " social_account @> '[{\"type\": \"facebook\"}]'::jsonb AND social_account @> '[{\"type\": \"twitter\"}]'::jsonb"
答案 0 :(得分:9)
我认为你需要这样的东西:
add_index :accounts, :email_provider, name: "index_accounts_on_email_provider_kind", using: :gin, where: "(email_provider -> 'email_provider' -> 'sparkpost' ->> 'smtp_host' = 'www.sparkpost.com') AND (email_provider -> 'email_povider' -> 'amazon ses' ->> 'smtp_host' = 'www.awses.com')"
我已将add_index
的第二个参数更改为:email_provider
,因为这是列名。另外,对于where
子句,我改变了
'email_provider' -> 'sparkpost' ->> 'smtp_host' = 'www.sparkpost.com'
到
email_provider -> 'email_provider' -> 'sparkpost' ->> 'smtp_host' = 'www.sparkpost.com'
因为->
运算符期望它将参数保留为json(b)值,但是您提供了一个字符串。所以例如email_provider -> 'email_provider'
从名为email_provider
的列中提取与email_provider
对应的值。请注意,最后一行可以更紧凑地编写为:
email_provider #>> '{email_provider,sparkpost,smtp_host}' = 'www.sparkpost.com'
使用从{josn(b)对象中提取“路径”的#>>
。所以add_index
语句可以写成:
add_index :accounts, :email_provider, name: "index_accounts_on_email_provider_kind", using: :gin, where: "(email_provider #>> '{email_provider,sparkpost,smtp_host}' = 'www.sparkpost.com') AND (email_provider -> '{email_povider,amazon ses,smtp_host}' = 'www.awses.com')"
对于你的第二个索引,你应该尝试类似:
where: " social_account -> 'social_account' @> '[{\"type\": \"facebook\"}]'::jsonb AND social_account -> 'social_account' @> '[{\"type\": \"twitter\"}]'::jsonb"
在这种情况下,我在第一种情况下对列进行了同样的思考。我还更改了@>
的正确参数,它必须是jsonb值。因此,您必须将其定义为JSON字符串,这需要字符串的双引号(还要注意我们必须为ruby转义它们),然后我将该字符串转换为jsonb
以获得所需类型。