Emke
Emke

Reputation: 357

Cassandra python driver ORM crashes when model contains set of UDT

I just recently started working with Cassandra db on python 3.

I have a model witch contains a set of user defined type:

# model.py
import uuid

from datetime import datetime
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.usertype import UserType

class Hobby(UserType):
    priority = columns.Integer()
    hobby = columns.Text()

    def __hash__(self):
        return hash(str(self.__class__) + ": " + str(self.__dict__))


class Person(Model):
    __keyspace__ = 'tracking'

    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    birth_date = columns.DateTime(default=datetime.now())
    hobbies = columns.Set(columns.UserDefinedType(Hobby))

File I am testing with looks like this:

#main.py
from settings import auth_provider
from models import *

from cassandra.cqlengine import connection
from cassandra.cqlengine.management import sync_table, sync_type

connection.setup(['127.0.0.1'], 'tracking', auth_provider=auth_provider, protocol_version=3)
sync_type('tracking', Hobby)
sync_table(Person)

Person.create(hobbies=[Hobby(priority=1, hobby='Coding'), ])
# Person.create(hobbies=[Hobby(priority=1, hobby='Coding'), Hobby(priority=2, hobby='Testing')])

for i in Person.objects().all():
    print(dict(i))

Now if I use the first Person.create with one hobby, data is read just fine.

However, if I use the second(commented out) Person.create with two hobbies, I receive the following exception:

cassandra.DriverException: Failed decoding result column "hobbies" of type set<frozen<hobby>>: '<' not supported between instances of 'Hobby' and 'Hobby'

Any suggestions for workarounds and am I doing it right ?

Upvotes: 2

Views: 623

Answers (1)

Emke
Emke

Reputation: 357

Ok so I actually spent so much time reading the docs that I didn't try the obvious, changing the Person model type of hobbies to List instead of Set.

Having hobbies = columns.List(columns.UserDefinedType(Hobby)) solves the issue.

Upvotes: 3

Related Questions