序列化bulkCreate不包括新添加的列

时间:2018-09-25 18:34:38

标签: javascript node.js postgresql express sequelize.js

我很难理解为什么bulkCreate不会包括我的两个新创建的列,也许是迁移?

我的新迁移如下:

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    queryInterface.addColumn('users', 'brand_id', {
      allowNull: true,
      type: Sequelize.INTEGER,
      defaultValue: null,
    });

    queryInterface.addColumn('users', 'store_id', {
      allowNull: true,
      type: Sequelize.INTEGER,
      defaultValue: null,
    });    

    return true;
  },

  down: (queryInterface, Sequelize) => {
    queryInterface.removeColumn('users', 'brand_id');
    queryInterface.removeColumn('users', 'store_id');
    return true;
  }
};

我有一个帮助程序功能,可以创建多个用户,以进行如下所示的测试:

const properties = { brand_id: 123 };
const user = [];
users.push(Object.assign({}, {
  name: chance.last(),
  email: chance.email(),
  password,
  access_key: uuid(),
}, properties));

const newUsers = await models.user.bulkCreate(users, { returning: true, logging: console.log });

日志记录的输出为:

INSERT INTO "users" ("id","name","email","password","access_key","created_at","updated_at") VALUES (DEFAULT,'Valente','letubdo@iwefa.fm','$2a$08$B5riQzA82ChwuH1q8HpGxOBK2uQj2m.BiHcEjytiox5yD.8u1fT5W','e62bf96c-0117-490f-9c80-b60e406238b0','2018-09-25 18:30:04.666 +00:00','2018-09-25 18:30:04.666 +00:00') RETURNING *;

即使我更改以下内容,您也会看到在查询中完全忽略了brand_id:

users.push(Object.assign({}, {
  name: chance.last(),
  email: chance.email(),
  password,
  access_key: uuid(),
  brand_id: 123,
}, properties));

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

事实证明,我没有将两个新列添加到用户模型:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var user = sequelize.define('user', {
    name: {
      allowNull: false,
      type: DataTypes.STRING
    },
    email: {
      allowNull: false,
      type: DataTypes.STRING
    },
    password: {
      allowNull: false,
      type: DataTypes.STRING
    },
    access_key: {
      allowNull: false,
      type: DataTypes.STRING
    },
    brand_id: {
      allowNull: true,
      true: DataTypes.INTEGER,
    },
    store_id: {
      allowNull: true,
      true: DataTypes.INTEGER,
    }    
  }, {
    underscored: true,
  });
  return user;
};