'Insert new user in database with sequelize does not save values
Im trying to create a new user with sequelize. When I run the code, I get this result :
Executing (default): ALTER TABLE `users` CHANGE `email` `email` VARCHAR(100) NOT NULL;
Executing (default): ALTER TABLE `users` CHANGE `password` `password` VARCHAR(100) NOT NULL;
Executing (default): ALTER TABLE `users` CHANGE `createdAt` `createdAt` DATETIME NOT NULL;
Executing (default): ALTER TABLE `users` CHANGE `updatedAt` `updatedAt` DATETIME NOT NULL;
Executing (default): SHOW INDEX FROM `users`
Executing (default): INSERT INTO `users` (`id`,`email`,`password`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?);
Table and model synced successfully !
It seems like the values are not saved in the database. What should I do to fix that ? If you need more details about the code feel free to ask in comment, I don't know what to add here to be more clear ^^.
My whole code :
const Sequelize = require("sequelize");
const connexion = require("../database");
const User = connexion.define(
"users",
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
email: {
type: Sequelize.STRING(100),
allowNull: false,
},
password: {
type: Sequelize.STRING(100),
allowNull: false,
},
},
{
freezeTableName: true
}
);
User.sync({ alter: true })
.then(() => {
const user = User.build({
email: "[email protected]",
password: "framboise",
});
return user.save();
})
.then((data) => {
console.log("Table and model synced successfully !");
})
.catch((err) => {
console.log("Sync error");
});
Solution 1:[1]
Problem solved by doing this :
User.create({ email: "[email protected]", password: "citrouille" })
.then((user) => {
console.log(user);
})
.catch((err) => {
console.log("Sync error");
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
