0
votes

I created a one table posts. When I make request SELECT:

return $this->db->query('SELECT * FROM "posts" WHERE "id" IN(:id) LIMIT '.$this->limit_per_page, ['id' => $id]);

I get error:

PRIMARY KEY column "id" cannot be restricted (preceding column "post_at" is either not restricted or by a non-EQ relation)

My table dump is:

CREATE TABLE posts (
  id       uuid,
  post_at  timestamp,
  user_id  bigint,
  name     text,
  category set<text>,
  link     varchar,
  image    set<varchar>,
  video    set<varchar>,
  content  map<text, text>,
  private  boolean,
  PRIMARY KEY (user_id,post_at,id)
  )
  WITH CLUSTERING ORDER BY (post_at DESC);

I read some article about PRIMARY AND CLUSTER KEYS, and understood, when there are some primary keys - I need use operator = with IN. In my case, i can not use a one PRIMARY KEY. What you advise me to change in table structure, that error will disappear?

1
just try interchanging user_id and id in primary key, Also to use = operator add a column which has value you know eg clom1 with value 1 - undefined_variable
There is not clom1 such as i know value, all content are different, i can create additional clom1 and add there is always value 1 for each row? - Alice
yes you will have to create a column which has fixed value which you can use - undefined_variable
I change that you advised: Have a query now: return $this->db->query('SELECT * FROM "posts" WHERE "index_field" = :index AND "id" IN(:id) LIMIT '.$this->limit_per_page, ['id' => $id, 'index' => 1]); Get error: 'Syntax_error: line 1:45 no viable alternative at input 'index' - Alice
Please, can you response me more extanded with a example? Thank you - Alice

1 Answers

1
votes

My dummy table structure

CREATE TABLE posts (
  id       timeuuid,
  post_at  timestamp,
  user_id  bigint,
  PRIMARY KEY (id,post_at,user_id)
  )
  WITH CLUSTERING ORDER BY (post_at DESC);

And after inserting some dummy data

I ran query select * from posts where id in (timeuuid1,timeuuid2,timeuuid3);

I was using cassandra 2.0 with cql 3.0