I have a table
CREATE TABLE countries
(
country_id INT GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) PRIMARY KEY,
country_name VARCHAR(255) NOT NULL
);
I am trying to send a post request to this to insert a row in my database. This is the body of my POST request {"country_name" = "PK"} I'm getting this error when I send the post request
I am using Oracle 21c btw
Error executing SQL query: Error: ORA-01400: cannot insert NULL into ("C##USER"."COUNTRIES"."COUNTRY_NAME")
Help: https://docs.oracle.com/error-help/db/ora-01400/at Protocol._processMessage (C:\Users\HP\Desktop\Ticketeer\node_modules\oracledb\lib\thin\protocol\protocol.js:172:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ThinConnectionImpl._execute (C:\Users\HP\Desktop\Ticketeer\node_modules\oracledb\lib\thin\connection.js:195:7) at async ThinConnectionImpl.execute (C:\Users\HP\Desktop\Ticketeer\node_modules\oracledb\lib\thin\connection.js:927:14) at async Connection.execute (C:\Users\HP\Desktop\Ticketeer\node_modules\oracledb\lib\connection.js:861:16) at async Connection. (C:\Users\HP\Desktop\Ticketeer\node_modules\oracledb\lib\util.js:165:14) at async AddNewCountry (C:\Users\HP\Desktop\Ticketeer\backend\controller\countriesController.js:126:13) { offset: 46, errorNum: 1400, code: 'ORA-01400' }
In my countries controller, I tried hard-coding req.body.country_name and assigning it a country name as a string. When I sent the request, that country name got entered in the database but the country_id that should've been 2 was 22. Here is my API code for AddNewCountry
AddNewCountry: async function (req, res){
let connection ;
try {
connection = await getConnection();
const query = `INSERT INTO countries (country_name) VALUES (:1)`;
const binds = [req.body.country_name];
const options = {
autoCommit: true,
};
await connection.execute(query,binds,options);
res.status(202).send("Added");
}
catch (error) {
console.error('Error executing SQL query:', error);
res.status(500).send('Internal Server Error');
}
finally {
if (connection) {
try {
// Release the connection when done
await connection.close();
} catch (error) {
console.error('Error closing database connection:', error);
}
}
}
},
I figured it out. It was an issue with how I was executing my POST request on Postman. Postman uses a default Text setting in the body of the POST requests instead of JSON, which made my req.body undefined. Changing it to JSON fixed the issue.