Closed frederikhors closed 6 years ago
@frederikhors Seed does not use model. Data is inserted directly into the table
How can we seed a table and call beforeCreate then ?
@jlevesque By importing and populating through sequelize model.
@nmihaica can I import and populate using sequelize models in the up
function?
So far I've tried to call MyModel.create()
and MyModel.beforeBulkCreate()
but I got this:
ERROR: Error: Migration 20181226195402-demo-user.js (or wrapper) didn't return a promise
If this function is not intended to use another thing that the queryInterface
API, where can we seed a table using sequelize models?
@KefDS Yes.
const User = require('@models').User
let users = [];
module.exports = {
up: function (queryInterface, Sequelize) {
return User.bulkCreate(users, {
validate:true,
individualHooks: true
})
},
down: function (queryInterface, Sequelize) {
}
};
Calling the sequelize model is a good solution, thanks @nmihaica.
But the problem is that queryInterface doesn't support hooks. I wanted to use it so I wouldn't have to import my model in the seed script. If your project is in ES6, like mine, you have to run the seeds with babel. I was trying to avoid that, because I wanted to be able to seed some basic data on the prod server, and this server only contains the builded application and the packages to run it. It doesn't seems logical to me to have babel on a live server and this also means that I had to push the src code on the prod server.
I did a database import instead hehe. My seed scripts are similar to nmihaica's example.
this is not woking for me `module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.bulkInsert('FirstLevelAdmins', [{ name: 'John', password: 'Doe', createdAt: new Date(), updatedAt: new Date(), email: 'johnDoe@test.com', id: '06c57dbd-b35f-4d53-9088-60d120bcdb81' }], { individualHooks: true }); },
down: function (queryInterface, Sequelize) { queryInterface.bulkDelete('FirstLevelAdmins', [{ }]) } }; `
@frederikhors seed does not work use model data inserted directly into table, I have same problem so I did something like this to bcrypt password import bcrypt first and then implement as follows
const salt = bcrypt.genSaltSync();
return queryInterface.bulkInsert('t_users', [{
username: 'admin',
password: bcrypt.hashSync('123456', salt),
created_at: new Date(),
updated_at: new Date()
}], {});
Hope this will solve your problem
@frederikhors seed does not work use model data inserted directly into table, I have same problem so I did something like this to bcrypt password import bcrypt first and then implement as follows
const salt = bcrypt.genSaltSync(); return queryInterface.bulkInsert('t_users', [{ username: 'admin', password: bcrypt.hashSync('123456', salt), created_at: new Date(), updated_at: new Date() }], {});
Hope this will solve your problem
it worked
I am using (async - await) and it is working perfectly. If you want to see how it looks, I'm posting the code below:
const bcrypt = require('bcryptjs');
module.exports = {
up: async queryInterface => {
return queryInterface.bulkInsert('users', [
{
name: 'Admin',
email: 'admin@root.com',
passwordHash: await bcrypt.hash('123456', 8),
type: 'admin',
createdAt: new Date(),
updatedAt: new Date(),
avatarId: null,
},
]);
},
down: queryInterface => {
return queryInterface.bulkDelete('users', null);
},
};
I did the same as @blindibrasil haha tamo junto amigao !
@blindibrasil Great! Thank you!
I'm trying to seed my DB with users.
I'm using seed like this:
In my model I have:
But when I try to use:
sequelize db:seed:all
it doesn't hash passwords.Why?