byrdr
byrdr

Reputation: 5467

Cassandra valid column names

I'm creating a api which will work on either mongo or cassandra, for that reason I'm using '_id' as a column name.

This should be a valid name according to the docs:

Keyspace, column, and table names created using CQL can only contain alphanumeric and underscore characters. User-defined data type names and field names, user-defined function names, and user-defined aggregate names created using CQL can only contain alphanumeric and underscore characters. If you enter names for these objects using anything other than alphanumeric characters or underscores, Cassandra will issue an invalid syntax message and fail to create the object.

However, when I run this statement:

CREATE TABLE users(_id: bigint, entires: map<timestamp, text>, PRIMARY KEY(_id));

I return the following error:

Invalid syntax at line 1, char 20

Is it possible to use underscores in column names?

Upvotes: 8

Views: 7112

Answers (2)

kostja
kostja

Reputation: 61538

You can create a column name starting with an underscore. Use quotes:

CREATE TABLE users("_id": bigint, entires: map<timestamp, text>, PRIMARY KEY("_id"));

The column name will be _id

Although you can, it does not mean that you should have such a column - you will need to continue using quotes in each query making it cumbersome:

SELECT "_id" FROM users;

Upvotes: 3

Aaron
Aaron

Reputation: 57748

Underscores in columns names? Yes. Column names starting with underscores? No.

From the CREATE TABLE documentation:

Valid table names are strings of alphanumeric characters and underscores, which begin with a letter.

Upvotes: 5

Related Questions