Cassandra未设置新列的默认值,以稍后在Python模型中添加



我的代码如下。

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()

第一次执行后,我可以在运行查询为。

时在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')

检查DB条目

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)

name作为null

有一排

,但我无法查询null值。

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

我获得了参考,如何搜索使用CQL的空/空字段的记录?

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

有什么方法可以将name中的null值设置为默认值''使用查询?

null小区实际上只是没有设置。而且,由于它是一个过滤操作,因此缺少数据,因此您无法查询。它不可扩展或有效地进行,因此它不会鼓励(或在这种情况下允许)。

回去并追溯地将值设置为所有先前创建的行将非常昂贵(必须读取所有内容,然后做很多写作)。

在应用方面很容易说明if name is null its ''

最新更新