3

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..

sameer manek
  • 735
  • 1
  • 11
  • 34

4 Answers4

5

It took a while to figure this out, Thank you all.

literature I referred to: this question AND this part of official sequelize documentation

Here is the code that works:

'use strict';
const bcrypt = require('bcryptjs');
const models = require('../models');
const User = models.User;
module.exports = {
    up: async (queryInterface, Sequelize) => {
        queryInterface.bulkInsert('Users', [{
            firstName: 'aname',
            lastName: 'alastname',
            email: 'someemail@somewhere.com',
            password: bcrypt.hashSync('poochies', 8),
            type: 'admin',
            createdAt: new Date(),
            updatedAt: new Date()
        }], {});

        const user = await User.findOne({
            where: {
                type: 'admin',
                email: 'someemail@somewhere.com'
            },
        });

        return await queryInterface.bulkInsert('Admins', [{
            id: user.id,
            phone: '+999999999999',
            status: true,
            createdAt: new Date(),
            updatedAt: new Date()
        }], {});
    },
    down: async (queryInterface) => {
        await queryInterface.bulkDelete('Admins', null, {});
        await queryInterface.bulkDelete('Users', null, {});
    }
};
Sgnl
  • 1,808
  • 22
  • 30
sameer manek
  • 735
  • 1
  • 11
  • 34
1

You are not waiting for the users insert to end before querying for them, so this:

const users = await queryInterface.sequelize.query(
   'SELECT id from Users;'
);

Is empty, and this users[0].id must be throwing a type error

Adding an await to your first queryInterface.bulkInsert should fix that

await queryInterface.bulkInsert('Users', [{ // ...
Aramil Rey
  • 3,387
  • 1
  • 19
  • 30
0

You need to add await to the first bulkInsert method call and save its results into userId variable. The bulkInsert promise is resolved with the first ID of inserted sequence, so you can use it to create an admin like this:

'use strict';
const bcrypt = require('bcryptjs');

module.exports = {
    up: async (queryInterface, Sequelize) => {
        const userId = await queryInterface.bulkInsert('Users', [{
            firstName: 'someone',
            lastName: 'awesome',
            email: 'someone@somewhere.com',
            password: bcrypt.hashSync('helloWorld', 8),
            type: 'admin',
            createdAt: new Date(),
            updatedAt: new Date()
        }], {});

        return queryInterface.bulkInsert('Admins', [{
            id: userId,
            phone: '+9999999999',
            status: true,
            createdAt: new Date(),
            updatedAt: new Date(),
        }]);
    },
    down: async (queryInterface) => {
        await queryInterface.bulkDelete('Admins', null, {});
        await queryInterface.bulkDelete('Users', null, {});
    }
};
Eugene Manuilov
  • 4,271
  • 8
  • 32
  • 48
0

You can receive an array of the inserted objects and use it in the admin insertion.

let ids = await queryInterface.bulkInsert('Users', [{
                    firstName: 'someone',
                    lastName: 'awesome',
                    email: 'someone@somewhere.com',
                    password: bcrypt.hashSync('helloWorld', 8),
                    type: 'admin',
                    createdAt: new Date(),
                    updatedAt: new Date()
                }], { returning: ['id'] });

let adminId = ids[0];

//Insert admin
Ahmed Elazazy
  • 454
  • 4
  • 10