首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sequelize :不能在DB表中创建关联值

sequelize :不能在DB表中创建关联值
EN

Stack Overflow用户
提问于 2018-09-07 18:08:50
回答 4查看 3.7K关注 0票数 3

我试图使用sequelize在我的数据库中创建一个关联,表是:用户和管理员。为此,我依赖于论坛上的这个答案。这就是我的种子:

代码语言:javascript
复制
'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.id:

代码语言:javascript
复制
const users = await queryInterface.sequelize.query(
    "SELECT id from Users"
);

console.log(users[0].id)

输出为undefined

但是数据又一次被传送到桌子上了!我知道这里发生了什么,但不知道怎么解决!

我还补充说,等待是up的第一种方法,但这并没有改变什么..

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-09-07 18:33:05

在查询用户之前,您不会等待用户插入结束,所以如下所示:

代码语言:javascript
复制
const users = await queryInterface.sequelize.query(
   'SELECT id from Users;'
);

为空,并且此users[0].id必须引发类型错误。

将一个await添加到您的第一个queryInterface.bulkInsert应该可以修复这个问题。

代码语言:javascript
复制
await queryInterface.bulkInsert('Users', [{ // ...
票数 1
EN

Stack Overflow用户

发布于 2018-09-08 10:38:21

花了一段时间才弄明白,谢谢大家。

我提到的文献:官方续写文档的这个问题这部分

下面是工作的代码:

代码语言:javascript
复制
'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, {});
    }
};
票数 5
EN

Stack Overflow用户

发布于 2019-11-25 20:43:54

需要将await添加到第一个bulkInsert方法调用中,并将其结果保存到userId变量中。bulkInsert承诺是用插入序列的第一个ID来解析的,所以您可以使用它来创建如下所示的管理:

代码语言:javascript
复制
'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, {});
    }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52227663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档