UserController.js
"use strict";
const User = use("App/Models/User");
const Hash = use("Hash");
class UserController {
async store() {
const user = new User();
user.username = "abc";
user.email = "[email protected]";
user.password = await Hash.make("abc");
return await user.save();
}
async show({ params }) {
const { id } = params;
return await User.find(id);
}
async update({ params }) {
const { id } = params;
const user = await User.find(id);
user.username = "abc";
user.email = "[email protected]";
user.password = await Hash.make("abc");
return await user.save();
}
}
module.exports = UserController;
I'm using fresh install of adonis, and mssql Azure as the database connection
after saving data using save() the created_at field updated to another value, check my video
Can you turn on Database debugging and share the output.
Set debug: true inside config/database.js file
this is the output of insert process
{ method: 'insert',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings:
[ '2018-05-18 00:42:21',
'[email protected]',
'$2a$10$q4UzR//c95355B2zkcchhO3LZK5wrVpEk12bnQ2Glw7FpCNUODRdm',
'2018-05-18 00:42:21',
'abc' ],
__knexQueryUid: '1480da19-7189-4d6f-a4d3-3a7aa0f35cea',
sql: 'insert into [users] ([created_at], [email], [password], [updated_at], [username]) output inserted.[id] values (?, ?, ?, ?,?)',
returning: 'id' }
This is output of update process
{ method: 'update',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings:
[ '$2a$10$lxcGka8yAMz7LpVgIQzFMujYi8ywg2iziYMiKGnzdm8zCeBgEoH1e',
'2018-05-18 07:42:21',
'2018-05-18 00:44:32',
4 ],
__knexQueryUid: '5bd1b48d-b57f-4bd1-b7c2-a8f9937ef9e7',
sql: 'update [users] set [password] = ?, [created_at] = ?, [updated_at] = ? where [id] = ?;select @@rowcount',
returning: '@@rowcount' }
I'm running on my machine which has timezone +07:00
If I am not wrong, it re-sets the same created_at datetime (should not be happening).
Also can you share the output of user.dirty ?
Finally, can you make sure that you don't have any Model hooks in place, that updates the created_at date?
yes, that should not be happening, but from the output it sets the same created_at + +07:00
the output of user.dirty
{ password: '$2a$10$1Ws4Pa5UQh3RlmvTPP/OseGxl79haCvrDdjPgYNaTRfibtHYZ3z3C' }
It's just the password changed
This is fresh install of adonis new so the model looks like this :
'use strict'
const Hash = use('Hash')
const Model = use('Model')
class User extends Model {
static boot () {
super.boot()
/**
* A hook to hash the user password before saving
* it to the database.
*/
this.addHook('beforeCreate', async (userInstance) => {
if (userInstance.password) {
userInstance.password = await Hash.make(userInstance.password)
}
})
}
/**
* A relationship on tokens is required for auth to
* work. Since features like `refreshTokens` or
* `rememberToken` will be saved inside the
* tokens table.
*
* @method tokens
*
* @return {Object}
*/
tokens () {
return this.hasMany('App/Models/Token')
}
}
module.exports = User
This error also occurred on my local mssql server. It seems adonis miss detect the timezone of the database. But I'm not so sure.
It's not error on mysql database.
Closing in favor of the PR.
I have this same problem .. I am using MSSQL and saving the records using the save () method. apparently when creating the record it saves with the correct dates .. but in the update process the new dates no longer match .. it also changes the created_at date whenever the save () method is executed ..
when i call delete method and change status using save() then created_at date update
Most helpful comment
I have this same problem .. I am using MSSQL and saving the records using the save () method. apparently when creating the record it saves with the correct dates .. but in the update process the new dates no longer match .. it also changes the created_at date whenever the save () method is executed ..