balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Model attribute updatedAt not updating with autoUpdatedAt: 'updated_at' #3821

Closed shanemileham closed 8 years ago

shanemileham commented 8 years ago

Sails version: 0.12.3 Node version: 5.7.0 NPM version: 3.6.0 Operating system: OSX 10.11.5


Expected Behavior:

After successfully updating a record using .update(), the record's updated_at field should auto-update to the current time.

_Note: I changed the autoUpdatedAt setting (also mentioned here) using autoUpdatedAt: 'updated_at' in config.models so the field is titled updated_at instead of updatedAt. It should still have the same functionality._

Actual Behavior:

The model successfully updates, but it does not update the updated_at field.

Steps to Reproduce:

    autoUpdatedAt: 'updated_at',
    migrate: 'alter'
module.exports = {
    test: function(req, res){
        Test.create()
        .then(function(created){
            sails.log('created');
            sails.log(created);

            //update
            Test.update(created, {})
            .then(function(updated){
                sails.log('updated');
                sails.log(updated[0]);

                Test.findOne(updated)
                .then(function(found){
                    sails.log('found');
                    sails.log(found);

                    res.end();
                });
            });
        });
    }
};
debug: created
debug: { createdAt: '2016-08-25T21:35:46.679Z',
  updated_at: '2016-08-25T21:35:46.679Z',
  id: 1 }
debug: updated
debug: { createdAt: '2016-08-25T21:35:46.679Z',
  updated_at: '2016-08-25T21:35:46.679Z',
  id: 1,
  updatedAt: '2016-08-25T21:35:46.698Z' }
debug: found
debug: { createdAt: '2016-08-25T21:35:46.679Z',
  updated_at: '2016-08-25T21:35:46.679Z',
  id: 1,
  updatedAt: '2016-08-25T21:35:46.698Z' }

So it looks like Sails is still setting the updatedAt field of the model on update instead of looking at the field name from sails.config.models. This will then not save to a SQL database because the updatedAt column does not exist.

Link to StackOverflow Question

ibrahimab commented 8 years ago

This also happens on sails 0.12.4, node 6.5.0, npm 3.10.3, Operating system centos 7, kernel: 3.10.0-327.28.3.el7.x86_64

sgress454 commented 8 years ago

Confirmed. Looks like a bug to me. @particlebanana?

ibrahimab commented 8 years ago

I did some research, this bug is in waterline v0.11.0 and was fixed in v0.11.3. So just updating the version to 0.11.3 will fix this.

Note: waterline is already on version v0.12.2, so maybe look into that?

shanemileham commented 8 years ago

So to clarify, the bug fix is in package.json to update and make the line "waterline": "~0.12.2"? Or at least 0.11.3?

On a personal level, I can fix this now by manually adding "waterline": "~0.12.2" to my package because npm uses flat structure, correct?

ibrahimab commented 8 years ago

I would not tempt a major version upgrade without testing everything. Leave a change like that to the core team. But you coould infact manually change that line to 0.11.3.

Node js does use flat structure nowadays but if you add it in your package.json my guess is it would just install both versions or you get an error. It has to change in sails' package.json

shanemileham commented 8 years ago

Right, understood. It would definitely need to be tested, so until then I wanted to make sure an individual change would work. I'll update it in sails' package.json to 0.11.3 then. Thank you for researching it and for your helpful comments! :)

shanemileham commented 8 years ago

After updating the sails package to include the updated waterline, I ran the same code as above, and it still doesn't work. NOTE: I used waterline 0.12.2 because 0.11.3 was not a valid install target according to npm.

I got the following code output and error:

debug: created
debug: { createdAt: '2016-09-19T03:05:58.554Z',
  updated_at: '2016-09-19T03:05:58.554Z',
  id: 19 }
Warning: a promise was created in a handler but was not returned from it
    at Deferred.toPromise (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/deferred.js:494:61)
    at Deferred.then (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/deferred.js:505:15)
    at TestProject/api/controllers/TestController.js:17:5
    at wrapper (TestProject/node_modules/sails-hook-orm/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
    at callback.success (TestProject/node_modules/sails-hook-orm/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (TestProject/node_modules/sails-hook-orm/node_modules/switchback/lib/factory.js:58:28)
    at TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/dql/create.js:248:9
    at TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:52:16
    at TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:269:32
    at TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:44:16
    at null.<anonymous> (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/schema.js:152:44)
    at fn (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:60:10)
    at TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:181:20
    at iterate (TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:262:13)
    at Object.async.forEachOfSeries.async.eachOfSeries (TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:281:9)
    at Object.async.forEachSeries.async.eachSeries (TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:214:22)
    at Object.runner.afterCreate (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:63:9)
    at after (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/dql/create.js:243:17)
    at TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/dql/create.js:230:68
    at wrapper (TestProject/node_modules/sails-hook-orm/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
    at callback.success (TestProject/node_modules/sails-hook-orm/node_modules/switchback/lib/normalize.js:33:31)
From previous event:
    at Deferred.then (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/deferred.js:505:27)
    at Object.module.exports.test (TestProject/api/controllers/TestController.js:11:4)
    at wrapper (TestProject/node_modules/sails/node_modules/lodash/index.js:3095:19)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at alwaysAllow (TestProject/node_modules/sails/lib/hooks/policies/index.js:224:11)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at module.exports (TestProject/node_modules/sails/lib/hooks/cors/clear-headers.js:14:3)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at sails.router.bind._middlewareType (TestProject/node_modules/sails/lib/hooks/csrf/index.js:148:11)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)

debug: updated
debug: { createdAt: '2016-09-19T03:05:58.554Z',
  updated_at: '2016-09-19T03:05:58.554Z',
  id: 19,
  updatedAt: '2016-09-19T03:05:58.577Z' }
Warning: a promise was created in a handler but was not returned from it
    at Deferred.toPromise (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/deferred.js:494:61)
    at Deferred.then (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/deferred.js:505:15)
    at TestProject/api/controllers/TestController.js:22:6
    at wrapper (TestProject/node_modules/sails-hook-orm/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
    at callback.success (TestProject/node_modules/sails-hook-orm/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (TestProject/node_modules/sails-hook-orm/node_modules/switchback/lib/factory.js:58:28)
    at TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/dql/update.js:234:9
    at TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:52:16
    at Object.async.forEachOf.async.eachOf (TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:236:30)
    at Object.async.forEach.async.each (TestProject/node_modules/sails-hook-orm/node_modules/async/lib/async.js:209:22)
    at TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/dql/update.js:225:13
    at updatedNestedAssociations (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/dql/update.js:262:12)
    at TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/dql/update.js:222:31
    at wrapper (TestProject/node_modules/sails-hook-orm/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
    at callback.success (TestProject/node_modules/sails-hook-orm/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (TestProject/node_modules/sails-hook-orm/node_modules/switchback/lib/factory.js:58:28)
    at afterwards (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/adapter/dql.js:244:14)
From previous event:
    at Deferred.then (TestProject/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/query/deferred.js:505:27)
    at Object.module.exports.test (TestProject/api/controllers/TestController.js:11:4)
    at wrapper (TestProject/node_modules/sails/node_modules/lodash/index.js:3095:19)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at alwaysAllow (TestProject/node_modules/sails/lib/hooks/policies/index.js:224:11)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at module.exports (TestProject/node_modules/sails/lib/hooks/cors/clear-headers.js:14:3)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at sails.router.bind._middlewareType (TestProject/node_modules/sails/lib/hooks/csrf/index.js:148:11)
    at routeTargetFnWrapper (TestProject/node_modules/sails/lib/router/bind.js:181:5)
    at callbacks (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (TestProject/node_modules/sails/node_modules/express/lib/router/index.js:100:7)

debug: found
debug: { createdAt: '2016-09-19T03:05:58.554Z',
  updated_at: '2016-09-19T03:05:58.554Z',
  id: 19,
  updatedAt: '2016-09-19T03:05:58.577Z' }
mikermcneil commented 8 years ago

@shanemileham Apologies for the slow reply. (@particlebanana and I have been mainly focused on the adapters/drivers on the Waterline front as we get ready for Sails v1.0) Looks like we just need to npm publish, but I want to run through the commits and make sure everything is solid first. We'll look at that this afternoon.

particlebanana commented 8 years ago

@shanemileham Published Waterline@0.11.4 which should have this patched.

shanemileham commented 8 years ago

Just tested it (and fixed my promise antipatterns), and it works! Thank you @mikermcneil and @particlebanana for your help. Looking forward to Sails v1.0! 😄

mikermcneil commented 8 years ago

@shanemileham you're welcome! Us too :)