burawi / tayr

an easy on-the-fly ORM for NodeJS, inspired by RedBeanPHP
http://burawi.github.io/tayr/
MIT License
20 stars 2 forks source link

new Date() value creating field of type 'INT(11)' rather than 'BIGINT' #1

Closed ZealJared closed 8 years ago

ZealJared commented 8 years ago

Let me just say, I love this module! That being said, I'm having a hard time tracking down why the code below doesn't create a 'BIGINT' field for 'dateCreated'

let userGroup = new T.tayr('userGroup', {
  dateCreated: new Date()
})

let user = new T.tayr('user', {
  firstName: 'Default',
  lastName: 'User'
})

user.setParent(userGroup, function () {
  console.log('User: ' + user.id + ' belongs to group: ' + userGroup.id)
})

userGroup.store(function () {
    user.store(function () {
      console.log('stored:' + userGroup + ' and: ' + user)
    })
})

Any ideas?

burawi commented 8 years ago

Hey Man! (apologies for the late reply) Glad that you like it. You can update the module to the latest version "0.3.2" and keep just the code below, it will do all the work you want:

let userGroup = new T.tayr('userGroup', {
  dateCreated: new Date()
})

let user = new T.tayr('user', {
  firstName: 'Default',
  lastName: 'User'
})

user.setParent(userGroup).then(function () {
  console.log('User: ' + user.id + ' belongs to group: ' + userGroup.id)
})

If that doesn't fix your problem, let me know what error do you get

ZealJared commented 8 years ago

You switched to promises! That's awesome.

I'm still getting an error on the dateCreated field. When I console.log(query.sql) out of mysql module's Connection.prototype.query() I get:

...
 CREATE TABLE IF NOT EXISTS userGroup ( id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY )
...
ALTER TABLE userGroup ADD dateCreated INT
...
INSERT INTO userGroup SET dateCreated = 1469547666197 ON DUPLICATE KEY UPDATE dateCreated = 1469547666197

Then I get the error:

Error: ER_WARN_DATA_OUT_OF_RANGE: Out of range value for column 'dateCreated' at row 1

So this is because a javascript Date() won't fit in an INT field, but from looking at your code, it seems like getDataType() should catch the Date object (with typeof and instanceof) and return a field data type of BIGINT.

In getDataType(v), if I console.log v along with the corresponding data type, I get:

2016-07-26T16:03:18.906Z 'BIGINT'
1469548998906 'INT'

So it appears getDataType is being called twice. The first time dateCreated is recognized as a Date object, the second time as a Number.

I notice mendValue has this line: if (v instanceof Date) return v.getTime() which could have the effect of converting a Date object to a Number. Do you think this is where the issue might be?

burawi commented 8 years ago

I Added this line in getDataType:

if (v > 2147483647) return "BIGINT";

does this solve the problem ?

ZealJared commented 8 years ago

Yes! Thank you so much. Keep up the good work!

burawi commented 8 years ago

You're welcome! And thank you too for your report and your encouragement! Don't hesitate to contribute