MySQL Forums
Forum List  »  Italian

creazione non voluta dei campi createdAt e updatedAt
Posted by: moreno moreno
Date: December 31, 2021 05:11AM

Ho uno strano problema utilizzando mysql con Nodejs.
NelModel ho definito le classi impostando il parametro "underscored: 0".
riporto ad esempio la classe "user"

module.exports = (sequelize, Sequelize) => {
const User = sequelize.define("users", {
id:{
// Sequelize module has INTEGER Data_Type.
type:Sequelize.INTEGER,
underscored: 0,

// To increment user_id automatically.
autoIncrement:true,

// user_id can not be null.
allowNull:false,

// For uniquely identify user.
primaryKey:true

},
cognome: {
type: Sequelize.STRING,
underscored: 0
},
nome: {
type: Sequelize.STRING,
underscored: 0
},
idStato: {
type: Sequelize.INTEGER,
underscored: 0
},
username: {
type: Sequelize.STRING,
underscored: 0
},
password: {
type: Sequelize.STRING,
underscored: 0
},
email: {
type: Sequelize.STRING,
underscored: 0
},
idRuolo: {
type: Sequelize.INTEGER,
underscored: 0
},
idRuolo_Day: {
type: Sequelize.INTEGER,
underscored: 0
},
noteutente: {
type: Sequelize.STRING,
underscored: 0
},
photo: {
type: Sequelize.STRING,
underscored: 0
},
remember_token: {
type: Sequelize.STRING,
underscored: 0
},
email_verified_at: {
type: Sequelize.DATE,
underscored: 0
},
key_utenti_operation: {
type: Sequelize.INTEGER,
underscored: 0
},
created_at: {
type: Sequelize.DATE,
underscored: 0
},
updated_at: {
type: Sequelize.DATE,
underscored: 0
},
},{
timestamps: true,
underscored: 0,
freezeTableName: true,
});

return User;
};

nel authcontroller utilizzo il metodo "login" per verificare le credenziali

exports.login = (req, res) => {
User.findOne({
where: {
email: req.body.username
}
})
.then(user => {
if (!user) {
return res.status(404).send({ message: "username inesistente" });
}

var passwordIsValid = bcrypt.compareSync(
req.body.password,
user.password
);

if (!passwordIsValid) {
return res.status(401).send({
accessToken: null,
message: "Invalid Password!"
});
}

var token1 = jwt.sign({ id: user.id }, config.secret, {
expiresIn: expiresToken // 86400 // 24 hours
});



const data = JSON.stringify(user);
console.log('backend - login --- data' + data);
var token = jwt.sign({data}, config.secret, {
expiresIn: config.expiresIn // 24 hours
})


// attenzione accessToken deve essere valorizzato co token1




res.status(200).send({
id: user.id,
username: user.username,
cognome: user.cognome,
user_ruolo: user.idruolo_day,
idruoloweb: user.idruolo_day,
email: user.email,
accessToken: token,
token_type: 'bearer',
expires_in: expiresToken
});
})
.catch(err => {
res.status(500).send({ message: err.message });
});
};


quando eseguo "User.findOne" viene generata la strnga sql con la select da eseguire.

il codice generato è il seguente

Executing (default): SELECT `id`, `cognome`, `nome`, `idStato`, `username`, `password`, `email`, `idRuolo`, `idRuolo_Day`, `noteutente`, `photo`, `remember_token`, `email_verified_at`, `key_utenti_operation`, `created_at`, `updated_at`, `createdAt`, `updatedAt` FROM `users` AS `users` WHERE `users`.`username` = 'misonsan' LIMIT 1;

Viene generato aggiungendo impropriamente anche i campi
`createdAt` e `updatedAt` che NON ESISTONO NEL Database

la struttura dei campi per la tabella users nel database è la seguente

CREATE TABLE `users` (
`id` int(4) NOT NULL,
`cognome` varchar(32) DEFAULT NULL,
`nome` varchar(32) DEFAULT NULL,
`photo` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '0.jpg',
`idStato` int(1) DEFAULT '0',
`tipoacc` int(1) NOT NULL DEFAULT '0' COMMENT 'tipoacc = 0 acc. provvisoria senza foto\r\ntipoacc = 1 acc. definitiva con upload foto\r\n\r\n',
`username` varchar(32) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`email` varchar(40) DEFAULT NULL,
`idRuolo` int(2) NOT NULL DEFAULT '0',
`idRuolo_Day` int(2) NOT NULL DEFAULT '0',
`idruoloweb` int(2) NOT NULL DEFAULT '0' COMMENT 'tabella T_ruolo_web',
`noteUtente` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`eseguitaAbilitazione` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'N',
`remember_token` varchar(100) DEFAULT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`key_utenti_operation` int(4) DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

quindi i campi createdAt e updatedAt NON ESISTONO.

come posso risolvere il problema potendo generare la strnga sql da far eseguire senza i campi createdAt e updatedAt che NON ESISTONO ?

Sicuramente è un parametro da valorizzare, ma non so dove intervenire ?

Grazie

Moreno

Options: ReplyQuote


Subject
Views
Written By
Posted
creazione non voluta dei campi createdAt e updatedAt
385
December 31, 2021 05:11AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.