2015-01-05 22 views
6

Bookshelf ile ilgili bir sorunum var, sütunlar için sorgu kullanmak istiyorum json type Tabloda sütunlar 'data' türü var json, bu elemanın tüm elemanlarını almak istiyorum 'team' = 'PSG'Bookshelf.js json sütunu ile nerede Postgresql

Ben testi:

collection.query('whereRaw', "data->'team'->>'PSG'"); 

bu hatayı

"argument of WHERE must be type boolean, not type text"

Ya

testi var
collection.query('where', "data", "#>", "'{team, PSG}'"); 

Ben bir rapor var

"The operator \"#>\" is not permitted"

Bence bu hatayı var https://github.com/tgriesser/bookshelf/issues/550

cevap

4

Kısa cevap:

collection.query('where', 'data', '@>', '{"team": "PSG"}'); 

Açıklama:

bir eleman foo gibi olurdu bunun için

------------------------ 
| id | attr    | 
------------------------ 
| 0 |{ "bar": "fooBar"} | 
------------------------ 
| 1 |{ "bar": "fooFoo"} | 
------------------------ 

ham sorgu nerede olduğunu, bir tablo foos olduğunu varsayalım.

select * from "foos" where "attr" @> '{"bar":"fooBar"}'; 

Artık Kitaplık'ta tablo foos'u temsil eden bir Foo modeliniz varsa, sorgu şöyle olmalıdır.

collection.query('where', 'data', '@>', '{"team": "PSG"}'); 

Umarım Zlatan o Onayladı gibi
Foo.where('attr', '@>', '{"bar":"fooBar"}').fetch().then(function(rows){}); 

Şimdi durum için o olmalıdır.

+0

Aynı sorguyu kullanıyorum, ancak yine de hata alıyorum, "İşlenmemiş reddetme hatası:" foo "yu seçin. *" Foo "dan" attr "@> $ 1 limit $ 2 - operatör mevcut değil: json @> bilinmiyor '. – Vasanth

+0

Lütfen dizne sorgunuzu debug bayrağıyla çalıştırabilir ve tam sorgunuzu gönderebilir misiniz? –