I am trying the following codes to create a keyspace and a table inside of it:
CREATE KEYSPACE IF NOT EXISTS books WITH REPLICATION = { 'class': 'SimpleStrategy',
'replication_factor': 3 };
CREATE TABLE IF NOT EXISTS books (
id UUID PRIMARY KEY,
user_id TEXT UNIQUE NOT NULL,
scale TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
reward map<INT,TEXT> NOT NULL,
image_url TEXT NOT NULL,
video_url TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
But I do get:
SyntaxException: line 2:10 no viable alternative at input 'UNIQUE' (...NOT EXISTS books ( id [UUID] UNIQUE...)
What is the problem and how can I fix it?
I see three syntax issues. They are mainly related to CQL != SQL.
The first, is that
NOT NULL
is not valid at column definition time. Cassandra doesn't enforce constraints like that at all, so for this case, just get rid of all of them.Next, Cassandra CQL does not allow default values, so this won't work:
Providing the current timestamp for
created_at
is something that will need to be done at write-time. Fortunately, CQL has a few of built-in functions to make this easier:In this case, I've invoked the
uuid()
function to generate a Type-4 UUID. I've also invokednow()
for the current time. Howevernow()
returns a TimeUUID (Type-1 UUID) so I've nested it inside of thetoTimestamp
function to convert it to a TIMESTAMP.Finally,
UNIQUE
is not valid.It looks like you're trying to make sure that duplicate
user_id
s are not stored with eachid
. You can help to ensure uniqueness of the data in each partition by addinguser_id
to the end of the primary key definition as a clustering key:This PK definition will ensure that data for books will be partitioned by
id
, containing multipleuser_id
rows.Not sure what the relationship is between books and users is, though. If one book can have many users, then this will work. If one user can have many books, then you'll want to switch the order of the keys to this:
In summary, a working table definition for this problem looks like this: