Skip to content

TypeCast blows up values that come after it in row #539

Open
@tomasikp

Description

@tomasikp

Issue #536 reminded me of a problem I ran into recently. In reality, I am typecasting timestamps to unix time, but for the sake of simplicity here i just typecast all timestamps to the string '1' as you can see the values that follow get garbled. I didn't notice this until recently because most of my timestamp values are the last column in a given table in my tables...

var mysql = require('mysql')
  , assert = require('assert');

var conn= mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test',
  typeCast: function (field, next) {
                if(field.type=='TIMESTAMP'){ 
                            return '1';
                }
                return next();
            }
});

conn.connect();

conn.query(
  "CREATE TABLE IF NOT EXISTS typecast_bug (id SERIAL,created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,active tinyint(1) DEFAULT '1', expired tinyint(1) DEFAULT '0')", 
  function(err) {
    if (err) throw err;


    conn.query('INSERT INTO typecast_bug SET ?', { 
      active:true
    }, function(err, result) {
      if (err) throw err;

      var id = result.insertId;

      conn.query('SELECT * FROM typecast_bug WHERE ?', { id: id }, function(err, row) {
        if (err) throw err;

       assert.equal(true, row[0].active);


        conn.end();

      });
    });
  }
);

Throws: AssertionError: true == "NaN"

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions