0

I'm new to astra db, and am having issues trying to insert a record into my table called 'usersnew' when using @astrajs/rest

This is the structure of my table:

CREATE TABLE mykeyspace.usersnew (
    id uuid PRIMARY KEY,
    email text,
    name text,
    userhandle text
)

1/ If I create a user and create/populate the ID field (the uuid), it works PERFECTLY!

    // create a new user with a document id -- seems to work!
      const { data, status } = await astraClient.put(
          "/api/rest/v2/keyspaces/mykeyspace/usersnew/ddd795a9-a9a2-49ca-a6a7-a29e1b039e20",
          {
            email: "abc@abc",
            name: "cliff",
            userhandle: "handlethis"
          }
        );

2/ If I try to create a user without specifying the ID field, it SADLY FAILS

    // creating a user without passing in a ID field
     const { data, status } = await astraClient.post(
          "/api/rest/v2/keyspaces/mykeyspace/usersnew", 
          {
            email: "aaa@aaa",
            name: "aaaa",
            userhandle: "handlethat"
           }
        );

This is the error I'm getting:

Astra Client Error: Error: Request Failed: [object Object]
Stack Trace: Request failed with status code 404

If someone could kindly help me that would be hugely appreciated. I've been trying to solve this for a whole day now with no luck at all.

I have been following the datastax documentation as well on this, but it's often wrong / inconsistent, sadly.

https://docs.datastax.com/en/astra-serverless/docs/develop/dev-with-rest.html

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23

1 Answers1

0

Your code is failing because you have not specified the ID. It is not possible to perform CRUD operations on Cassandra without specifying the partition key.

In Cassandra, the partition key is used to partition the data meaning that the partition key is used to determine which node(s) the data partition (record) gets stored.

In your case, id is the partition key. You have to specify an id or Cassandra doesn't know which partition the data belongs to.

As a side note, it is not necessary to use "artificial" keys like a UUID. We recommend using "natural" keys like email addresses as the partition key since they are universally unique. Similarly, you can use userhandle as the partition key if they are unique across the whole domain.

Here are some examples for partitioning with natural keys instead of artificial IDs:

CREATE TABLE users_by_email (
    email text,
    name text,
    userhandle text,
    PRIMARY KEY (email)
)
CREATE TABLE users_by_handle (
    userhandle text,
    email text,
    name text,
    PRIMARY KEY (userhandle)
)
Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
  • Erick! Thank you so much. for your quick response That makes a lot of sense! Let's say I want to do the same for a posts table (a social media table), are you also suggesting the PK would be userid and createdAt instead of postid (an artificially generated key)? If I opt for using postID (an artifically generated key), would i have to generate that in the front end code, and if so, how can i ensure uniqueness before writing to the DB using @astrajs/rest logic? Thank you again for your quick response, it's much appreciated! – Rick David Aug 21 '23 at 23:01
  • If you're using `postid`, you'll have to generate it in your app BEFORE writing to the database. And you will need to implement the algorithm in your app to make it unique, for example as a time UUID. Cheers! – Erick Ramirez Aug 21 '23 at 23:04
  • Thank you very much, Erick. You're a superstar!! One more question. I want to build a social media site using serverless functions in netlify with astra db serverless. I'd like to use the cassandra-driver as it can do batch statements which is an advantage over astrajs/rest. My issue is it takes 5 seconds to connect to the cassandra-driver in the serverless function. Much slower than astrajs/rest. Is if fair to say using the cassandra-driver isn't the right approach given it takes 5 seconds to connect? Thank you again for being so kind as to answer my questions! – Rick David Aug 21 '23 at 23:18
  • A friendly request that you should post a new question since it's completely different from this post. If we keep responding to follow up questions in the comments section, it will be a never-ending thread. Cheers! – Erick Ramirez Aug 22 '23 at 01:29
  • Sure, will do Erick. I'll post the link here once I've posted it. Thank you again for all your help, you are a life saver :) – Rick David Aug 22 '23 at 01:40
  • And one more thing since you're new to Stack Overflow -- please "accept" my answer if it solved your issue. For guidance, see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). Cheers! – Erick Ramirez Aug 22 '23 at 04:36
  • Done, thanks again Erick! – Rick David Aug 22 '23 at 05:59