I pass an SQL UPDATE statement a JSON object with less "columns" than the table. Why does the SQL statement update values of the "missing" columns as NULL?
I passing a JSON object to my object constructor which then sets the JSON key/values as its own.
If my JSON is missing a key-value pair (column), the constructor should not set its corresponding local variable. So why does the SQL UPDATE statement still update the missing "column" as NULL?
//---1st File---//
const Foo = require('.Foo');
let json = {
oneKey: 'oneValue',
twoKey: 'twoValue',
// threeKey: 'threeValue' // <--- if this is commented out, ...
};
let foo = new Foo(json);
Foo.update(foo, callback);
function callback(err, data) {
if (err) console.log(err);
console.log(data);
}
//---End 1st File---//
//---2nd File---//
const pool = require('.sql');
let Foo = function(that) {
this.oneKey = that.oneKey;
this.twoKey = that.twoKey;
this.threeKey = that.threeKey; // <--- then this local var should not be set in memory. Inspecting the object does not give threeKey with a empty value so I assume its not set in memory...
};
Foo.updateOne = function(foo, callback) {
let sql = `UPDATE table_name SET ? WHERE oneKey = ?`;
pool.query(sql, [foo, foo.oneKey], callback); // <--- but after query, column threeKey is updated as NULL. Why?
};
module.exports = Foo;
//---End 2nd File---//
//---This also does not work for 2nd File---//
const pool = require('.sql');
let Foo = function(that) {
if (!that.oneKey) this.oneKey = that.oneKey; // <--- I tried doing something like this in case the above wasn't quite right...
if (!that.twoKey) this.twoKey = that.twoKey;
if (!that.threeKey) this.threeKey = that.threeKey;
};
Foo.updateOne = function(foo, callback) {
let sql = `UPDATE table_name SET ? WHERE oneKey = ?`;
pool.query(sql, [foo, foo.oneKey], callback); // <--- but after query, the column still updates as NULL.
};
module.exports = Foo;
//---End Example---//
What I expected to happen: SQL statement only updates column oneKey and twoKey, since threeKey is not defined.
What actually happens: SQL statement updates oneKey and twoKey (as it should), but also updates threeKey to NULL.