【问题标题】:Sequelize db seeding with async model query使用异步模型查询对数据库播种进行续集
【发布时间】:2018-01-19 20:22:51
【问题描述】:

我有 2 个相关的表,我想在这些表上设置种子。用户播种机工作正常,并且 在我的第二个种子文件中,我查询所有用户,然后将每个用户附加到 users_information 表中。这是我的代码。

var chance = require('chance').Chance();
const User = require('../models/User');

module.exports = {
  up: function (queryInterface, Sequelize) {
    User.findAll()
      .then(users => {
        var usersInfo = [];
        for(let user of users) {
          var userInfo = {
            user_id: user.id,
            first_name: user.username,
            middle_name: chance.last(),
            last_name: chance.last(),
            address: chance.address()
          };
          usersInfo.push(userInfo);
        }

        return queryInterface.bulkInsert('users_information', usersInfo);
      })
      .catch(error => console.error(error));
  },

  down: function (queryInterface, Sequelize) {
      return queryInterface.bulkDelete('users_information', null, {});
  }
};

当我运行 db:seed:all 命令时,它运行时没有错误,并且 users_information 表上的数据为空。

播种机在没有模型查询的情况下工作。只是为了测试我的播种器文件是否有效。

up: function (queryInterface, Sequelize) {
     var usersInfo = [];

     for(var i = 1; i <= 10; i++) {
       var userInfo = {
         user_id: i,
         first_name: chance.first(),
         middle_name: chance.last(),
         last_name: chance.last(),
         address: chance.address()
       };
       usersInfo.push(userInfo);
     }
     return queryInterface.bulkInsert('users_information', usersInfo);
},

这是我的桌子

用户表

- id (PK)
- username
- user_type
- is_active

用户信息表

- id (PK)
- user_id (FK)
- first_name
- last_name
- address

有什么建议或想法吗?谢谢!

【问题讨论】:

  • 我也尝试添加 user = user.get({plain: true});在用户循环中,仍然无法正常工作

标签: sequelize.js sequelize-cli


【解决方案1】:

我遇到了类似的问题。我的问题是通过返回一个承诺来解决的。 我认为 sequelize 期望得到回报。它将等到一个承诺被解决或被拒绝。如果一个承诺没有得到回报,我认为它假设一切都“完成”和“好”。您的第一个示例没有返回承诺。它只是“运行”并存在而无需等待所有异步代码完成(又名 User.findAll())。

解决方案: 我认为,如果您在 up 方法中返回一个承诺并在“完成”时解决它,我认为这会奏效。示例:

  up: function (queryInterface, Sequelize) {
    // return a new promise that sequelize will wait for...
    return new Promise((resolve, reject)=>{
         User.findAll()
              .then(users => {
                var usersInfo = [];
                for(let user of users) {
                  var userInfo = {
                    user_id: user.id,
                    first_name: user.username,
                    middle_name: chance.last(),
                    last_name: chance.last(),
                    address: chance.address()
                  };
                  usersInfo.push(userInfo);
                }
                // Notice we are not returning this.
                queryInterface.bulkInsert('users_information', usersInfo);
                resolve('Done');
              })
              .catch((error) => {
                console.error(error);
                reject(error)
              });
    }
  },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2011-09-29
    • 2017-07-17
    • 1970-01-01
    相关资源
    最近更新 更多