Tuesday 30 May 2017

How can I send a POST request without sending an auto incrementing primary key in express

I am writing a simple API which I have been testing using Postman. I have an auto incrementing primary key (CustomerTypeID) for my "customer_type" table stored in MySQL. For practical reasons I need to be able to create records in this table without sending a CustomerTypeID. When I send the following POST request using Postman:

{
  "CustomerType": "testing"
}

The updated table shows a new row with CustomerTypeID of 2 and a CustomerType of NULL.

Below is a snippet of code in my Express API which shows this specific query and how the routing for this POST request works.

var db = require('../dbconnection.js');
var CustomerType = {
  addCustomerType:function(CustomerType,callback) {
    return db.query("INSERT INTO customer_type (CustomerType) VALUES (?)", [CustomerType.CustomerType], callback);
  }
};
module.exports = CustomerType;

I know that I could change the query to say

INSERT INTO customer_type (CustomerTypeID, CustomerType) VALUES (?,?);

and that would fill both columns. But, I do not know how to leave out the CustomerTypeID column as it will be a number that the end user will have no way of knowing.



via Colin Harrison

No comments:

Post a Comment