0

My get request seems to work. I used to have id's like this 1,2,3,4 Now I want to make them as uuid I've made a table for this

CREATE TABLE todo_list(
    todo_id UUID NOT NULL PRIMARY KEY,
    description VARCHAR(255),
);
router.post("/todo", async (req, res) => {
    try {
       const { description } = req.body;
       const newTodo = await pool.query("INSERT INTO todo_list (description) VALUES($1)"
       , [description]);

        res.json(newTodo.rows[0]);
    } catch (err) {
        console.log(err.message);
    }
});

But my post request wont work anymore after I changed ID to uuid. How can I fix this?

Dave
  • 1
  • 1
  • 9
  • 38

1 Answers1

1

You're not providing a default value for todo_id, and aren't passing it into insert statement. You could either generate a UUID at the application layer, or change the DDL from:

CREATE TABLE todo_list(
    todo_id UUID NOT NULL PRIMARY KEY,
    description VARCHAR(255),
);

to:

CREATE TABLE todo_list(
    todo_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    description VARCHAR(255),
);

in order to have PostgreSQL dynamically generate a UUID.

related

Alex
  • 34,899
  • 5
  • 77
  • 90