2016-12-13 21 views
2

我是Cassandra的新手,我試圖使用CQLEngine ORM來更新包含UDT的set列,但是我不能和文檔沒有提到有關自定義類型的任何內容。更新用CQLEngine設置的UDT

我的代碼是;

class MyType(UserType): 

    val = columns.Text() 
    point = columns.Integer() 
    key = columns.Text() 

    def __init__(self, val, point, key, **values): 
     super().__init__(**values) 
     self.val = val 
     self.point = point 
     self.key = key 


class MyModel(Model): 

    myid = columns.UUID(primary_key=True) 
    set_clm = columns.Set(columns.Integer) 
    mytype = columns.Set(UserDefinedType(MyType)) 

    def __init__(self, set_clm, mytype, **values): 
     super().__init__(**values) 
     self.myid = uuid4() 
     self.set_clm = set_clm 
     self.mytype = mytype 



s = MyModel.objects(myid="2b3adb7d-9e68-49fc-9aa0-26dbec607f9d").update(
    mytype__add=set(MyType(val="1", point=2, key="3")) 
) 

爲MyModel最初持有集中的空,但是當我嘗試更新它,我得到以下錯誤:

cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="Invalid set literal for mytype: value 'point' is not of type frozen<mytype>"

'point' is not of type frozen<mytype> - >這部分,每當我重新運行該代碼(下一個隨機變化我會跑的時間,我會得到'val'列的相同錯誤等)。

任何人都可以幫助我如何添加UDT設置?

回答

0

好的。我解決了它。我正在寫給那些在Google上找到它的人。

這是增加了一套正確的方法:mytype__add={MyType(val="1", point=2, key="3")}

,並實施的MyType的__hash__功能,如:

def __hash__(): 
    return hash(self.__repr__()) 

但一個更聰明的__hash__功能。這只是一個例子。希望它對別人有幫助。