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

2.8k Views Asked by At

I have code like below.

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

After first execution, I can see the record in db when run query as.

cqlsh> select * from test.other_table;

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

(1 rows)

After this, I added new column name in OtherModel it and run same program.

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

When check db entry

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)

There is one row with name as null.

But I can't query on null value.

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

I got reference How Can I Search for Records That Have A Null/Empty Field Using CQL?.

When I set default='' in the Model, why it not set for all the null value in table?

Is there any way to set null value in name to default value '' with query?

1

There are 1 best solutions below

2
On BEST ANSWER

The null cell is actually it just not being set. And the absence of data isn't something you can query on, since its a filtering operation. Its not scalable or possible to do efficiently, so its not something C* will encourage (or in this case even allow).

Going back and retroactively setting values to all the previously created rows would be very expensive (has to read everything, then do as many writes). Its pretty easy in application side to just say if name is null its '' though.