3

我是 Cassandra 的新手,我正在尝试使用 CQLEngine ORM 来更新包含 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 最初在 set 中保存 NULL 但是当我尝试更新它时,我收到以下错误:

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 集?

4

1 回答 1

1

好的。我已经解决了。我正在为那些在谷歌上找到它的人写下来。

这是添加到集合的正确方法:mytype__add={MyType(val="1", point=2, key="3")}

并实现__hash__MyType 的功能,例如:

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

但具有更智能的__hash__功能。这只是一个例子。希望对其他人有所帮助。

于 2016-12-14T21:17:44.627 回答