sequelize:无法在数据库表中播种关联值 [英] sequelize : cannot seed association values in DB table

查看:118
本文介绍了sequelize:无法在数据库表中播种关联值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sequelize将关联播种到我的数据库中,这些表是:Users和Admins.为此,我依靠论坛上的此答案.所以这是我的种子:

'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, {});
    }
};

现在,用户表中的数据是完整字段,但管理表仍然为空

我尝试使用以下代码打印出users [0] .id:

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

console.log(users[0].id)

输出为undefined

但是数据再次被馈送到表中!我知道这里发生了什么,但不知道如何解决!

P.S. 我还添加了等待up的第一种方法,但这并没有改变..

解决方案

花了一段时间才弄清楚,谢谢大家.

我提到的文学是: 此问题这部分是官方续集文档

这是有效的代码:

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

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

        return await queryInterface.bulkInsert('Admins', [{
            id: user.id,
            phone: '+919409662665',
            status: true,
            createdAt: new Date(),
            updatedAt: new Date()
        }], {});
    },
    down: async (queryInterface) => {
        await queryInterface.bulkDelete('Admins', null, {});
        await queryInterface.bulkDelete('Users', null, {});
    }
};

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

解决方案

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: 'sameer',
            lastName: 'manek',
            email: 'sameermanek@hotmail.com',
            password: bcrypt.hashSync('poochies', 8),
            type: 'admin',
            createdAt: new Date(),
            updatedAt: new Date()
        }], {});

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

        return await queryInterface.bulkInsert('Admins', [{
            id: user.id,
            phone: '+919409662665',
            status: true,
            createdAt: new Date(),
            updatedAt: new Date()
        }], {});
    },
    down: async (queryInterface) => {
        await queryInterface.bulkDelete('Admins', null, {});
        await queryInterface.bulkDelete('Users', null, {});
    }
};

这篇关于sequelize:无法在数据库表中播种关联值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆