2015-06-17 5 views
5

यहां JSONB के लिए वर्तमान कोड बनाने सूचकांक है।एसक्यूएलकेमी पर जीआईएन का उपयोग कर जेसनबी इंडेक्स कैसे बनाएं?

Index("mytable_data_idx_id_key", Mytable.data['id'].astext, postgresql_using='gin') 

लेकिन मुझे यह त्रुटि मिली।

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) data type text has no default operator class for access method "gin" 
HINT: You must specify an operator class for the index or define a default operator class for the data type. 
[SQL: "CREATE INDEX event_data_idx_id_key ON event USING gin ((data ->> 'id'))"] 

क्या एसक्यूएलकेमी पर इंडेक्स बनाने का कोई तरीका है?

उत्तर

1

PostgreSQL http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#operator-classes पर विशिष्ट SQLAlchemy डॉक्स एक postgresql_ops शब्दकोश "ऑपरेटर वर्ग" PostgreSQL द्वारा प्रयोग किया जाता प्रदान करने के लिए उल्लेख, और इसके उपयोग को दर्शाता हुआ इस उदाहरण प्रदान करते हैं:

Index('my_index', my_table.c.id, my_table.c.data, 
         postgresql_ops={ 
          'data': 'text_pattern_ops', 
          'id': 'int4_ops' 
         }) 

प्रयोग से ऐसा प्रतीत होता है कि आप की जरूरत है यदि आप अभिव्यक्ति अनुक्रमणिका के लिए "ऑपरेटर क्लास" निर्दिष्ट करना चाहते हैं तो text() अनुक्रमणिका विवरण का उपयोग करें। तो,

db.Index(
    'ix_sample', 
    sqlalchemy.text("(jsoncol->'values') jsonb_path_ops"), 
    postgresql_using="gin") 

... एक ORM मॉडल के लिए __table_args__ में एक jsonb क्षेत्र है कि तार की एक सरणी में शामिल है पर एक जिन सूचकांक निर्दिष्ट करता है, और कहा कि, यानी JSON में तार में से किसी पर मिलान कुशल लुकअप के लिए अनुमति देता है सरणी क्षेत्र है कि इस तरह दिखता है:

{ 
    "values": ["first", "second", "third"], 
    "other": "fields", 
    "go": "here" 
} 

PostgreSQL में @> ऑपरेटर का उपयोग कुछ इस तरह दिखेगा क्वैरी:

import sqlalchemy 
from sqlalchemy.dialects import postgresql 

query = session.query(MyModel).filter(
    sqlalchemy.type_coerce(MyModel.jsoncol['values'], postgresql.JSONB) 
    .contains(sqlalchemy.type_coerce("second", postgresql.JSONB))) 
results = query.all() 
+0

मैं trie डी लेकिन मुझे त्रुटि मिली। 'sqlalchemy.exc. प्रोग्रामिंग त्रुटि: (psycopg2.ProgrammingError) ऑपरेटर वर्ग" text_pattern_ops "डेटा प्रकार jsonb' –

+0

स्वीकार नहीं करता है मैंने अपने उत्तर को और उदाहरणों के साथ अपडेट किया है –

संबंधित मुद्दे