我从这里使用官方示例:http://datastax.github.io/python-driver/cqlengine/models.html#model-inheritance
将继承和UDF结合使用时,它似乎不起作用。我想念什么?
它会引发KeyError,我不知道为什么,我尝试了很多没有成功的事情
谢谢
# coding=utf-8
from uuid import uuid4
from cassandra.cqlengine import connection
from cassandra.cqlengine.columns import Text, UserDefinedType, UUID, Float
from cassandra.cqlengine.management import sync_table
from cassandra.cqlengine.models import Model
from cassandra.cluster import Cluster
from cassandra.cqlengine.usertype import UserType
cluster = Cluster(['127.0.0.1'])
session = cluster.connect()
session.execute(
"CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};")
###### Copied from example
# http://datastax.github.io/python-driver/cqlengine/models.html#model-inheritance
# Only thing changed is
# class Pet(Model) changed to Pet(UserType) to nest a Pet in a Person
#
#
class Pet(UserType):
owner_id = UUID(primary_key=True)
pet_id = UUID(primary_key=True)
pet_type = Text(discriminator_column=True)
name = Text()
def eat(self, food):
pass
def sleep(self, time):
pass
class Cat(Pet):
__discriminator_value__ = 'cat'
cuteness = Float()
def tear_up_couch(self):
pass
class Dog(Pet):
__discriminator_value__ = 'dog'
fierceness = Float()
def bark_all_night(self):
pass
class Person(Model):
__keyspace__ = 'test'
name = Text(primary_key=True)
pet = UserDefinedType(Pet)
# the list of hosts will be passed to create a Cluster() instance
connection.setup(['127.0.0.1'], "test", protocol_version=3)
# ...and create your CQL table
sync_table(Person)
Person.create(
name='john',
pet=Dog(
fierceness=0.11
)
)