Cassandra Database issue while im Insert into a new column

19 Views Asked by At

I've just installed Cassandra database because as I saw it provides dynamically creating of new columns in a table without need to altering it But when I make a dummy Insert like:

INSERT INTO mytable (id, name, age, description) VALUES (uuid(), 'John Doe', 25, 'Test');

where the mytable has id,name,age and it doesnt have description column it returns me error:

InvalidRequest: Error from server: code=2200 [Invalid query] message="Undefined column name description in table mykeyspace.mytable"

Isnt it supposed to dynamically make the description column inside the mytable?

2

There are 2 best solutions below

0
Madhavan On BEST ANSWER

Without the existence of the description column, it won't auto-create it for you within mytable.

You could do,

ALTER TABLE mytable ADD description TEXT;

and your INSERTs with the description column will begin working again.

0
Aaron On

Isnt it supposed to dynamically make the description column inside the mytable?

No. It is a common misconception that Cassandra is "schemaless," when it does in fact require and enforce a schema. Originally, Apache Cassandra supported a schemaless key/value approach to data representation, but that hasn't been the case for several years now.

In your case, you will need to define your table similar to this:

CREATE TABLE mykeyspace.mytable (
    id UUID,
    name TEXT,
    age INT,
    description TEXT,
    PRIMARY KEY(id)
);

Although truth be told, unless you plan on querying that data by id (a UUID), using a primary key definition of PRIMARY KEY(name,id) might be a better approach. That will allow querying by the name column, instead.