I'm trying to make a primary key for my buy, sell changes. I'm adding the coin name with the rate for my key and inserting my buy and sell into different tables. I keep getting duplicate keys for type 0 which means to add a order. I thought the rate should be unique. Is this something to do with cloud flare serving me cached copy? Bittrex's websocket serving duplicate data?
I have the under standing that if another order comes in with the same rate as one that I've already added it ether has to be Remove : 1 or a Update 2
var buyers = data_for.Buys;
var sellers = data_for.Sells;
var fills = data_for.Fills;
buyers.forEach(function (buyers) {
type = buyers.Type;
if (type === 0) {
// get BTC amount for trade
buyersRate = buyers.Rate.toFixed(8);
id = data_for.MarketName+buyersRate;
btc_amount = buyers.Rate * buyers.Quantity;
record = {
id: id,
coin_id: data_for.MarketName,
rate: buyers.Rate,
quantity: buyers.Quantity,
btc_total: btc_amount,
time_stamp: buyers.TimeStamp
};
conn.query('INSERT INTO buy_orders SET ?', record, function (err) {
if (err) throw err;
});
} else if (type === 1) {
buyersRate = buyers.Rate.toFixed(8);
id = data_for.MarketName+buyersRate;
conn.query('DELETE FROM buy_orders WHERE id = ?', [id], function (err) {
if (err) throw err;
});
} else if (type === 2) {
buyersRate = buyers.Rate.toFixed(8);
id = data_for.MarketName+buyersRate;
btc_amount = buyers.Rate * buyers.Quantity;
conn.query('UPDATE buy_orders SET quantity = ?, btc_total = ?, time_stamp = ? WHERE id = ?', [buyers.Quantity, btc_amount, buyers.TimeStamp, id], function (err) {
if (err) throw err;
});
}
});
});
}