I am trying to seed an association into my db using sequelize, the tables being: Users and Admins. For this I am relying on this answer on the forum. so here is my seed:
'use strict';
const bcrypt = require('bcryptjs')
module.exports = {
up: async (queryInterface, Sequelize) => {
queryInterface.bulkInsert('Users', [{
firstName: 'someone',
lastName: 'awesome',
email: 'someone@somewhere.com',
password: bcrypt.hashSync('helloWorld', 8),
type: 'admin',
createdAt: new Date(),
updatedAt: new Date()
}], {});
const users = await queryInterface.sequelize.query(
'SELECT id from Users;'
);
return await queryInterface.bulkInsert('Admins', [{
id: users[0].id,
phone: '+9999999999',
status: true, createdAt: new Date(),
updatedAt: new Date()
}]);
},
down: async (queryInterface) => {
await queryInterface.bulkDelete('Admins', null, {});
await queryInterface.bulkDelete('Users', null, {});
}
};
now, The data in user table is field up perfectly but the admin table remains empty
EDIT:
I tried to print out the users[0].id with the following code:
const users = await queryInterface.sequelize.query(
"SELECT id from Users"
);
console.log(users[0].id)
the output was undefined
but the data was once again fed to the table! I know what is happening here, but don't know how to resolve!
P.S. I also added await for the very first method of the up, but this changed nothing..