cassandra 未为稍后在 python 模型中添加的新列设置默认值

cassandra not set default value for new column added later in python model

我有如下代码。

from uuid import uuid4
from uuid import uuid1

from cassandra.cqlengine import columns, connection
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.management import sync_table


class BaseModel(Model):
    __abstract__ = True

    id = columns.UUID(primary_key=True, default=uuid4)
    created_timestamp = columns.TimeUUID(primary_key=True,
                                         clustering_order='DESC',
                                         default=uuid1)
    deleted = columns.Boolean(required=True, default=False)

class OtherModel(BaseModel):
    __table_name__ = 'other_table'



if __name__ == '__main__':
    connection.setup(hosts=['localhost'],
                     default_keyspace='test')
    sync_table(OtherModel)

    OtherModel.create()

第一次执行后,运行 query as.

可以看到db中的记录
cqlsh> select * from test.other_table;

 id                                   | created_timestamp                    | deleted
--------------------------------------+--------------------------------------+---------
 febc7789-5806-44d8-bbd5-45321676def9 | 840e1b66-cc73-11e6-a66c-38c986054a88 |   False

(1 rows)

在此之后,我在 OtherModel 和 运行 相同的程序中添加了新列 name

class OtherModel(BaseModel):
    __table_name__ = 'other_table'
    name = columns.Text(required=True, default='')




if __name__ == '__main__':
    connection.setup(hosts=['localhost'],
                     default_keyspace='test')
    sync_table(OtherModel)

    OtherModel.create(name='test')

检查数据库条目时

cqlsh> select * from test.other_table;

 id                                   | created_timestamp                    | deleted | name
--------------------------------------+--------------------------------------+---------+------
 936cfd6c-44a4-43d3-a3c0-fdd493144f4b | 4d7fd78c-cc74-11e6-bb49-38c986054a88 |   False | test
 febc7789-5806-44d8-bbd5-45321676def9 | 840e1b66-cc73-11e6-a66c-38c986054a88 |   False | null

(2 rows)

有一行 namenull

但我无法查询 null 值。

cqlsh> select * from test.other_table where name=null;
InvalidRequest: code=2200 [Invalid query] message="Unsupported null value for indexed column name"

我得到参考 How Can I Search for Records That Have A Null/Empty Field Using CQL?

当我在模型中设置 default='' 时,为什么它没有为 table 中的所有 null 值设置?

有没有办法通过查询将 name 中的 null 值设置为默认值 ''

空单元格实际上只是没有被设置。缺少数据不是您可以查询的内容,因为它是一种过滤操作。它不可扩展或不可能高效地执行,因此 C* 不会鼓励(或者在这种情况下甚至允许)。

返回并为所有先前创建的行追溯设置值将非常昂贵(必须读取所有内容,然后执行尽可能多的写入)。不过在应用程序方面很容易只说 if name is null its ''