简体   繁体   中英

Sequelize bulkCreate not including newly added columns

I'm having difficulty understanding why bulkCreate will not include my two newly created columns, perhaps it's the migration?

My new migration is as follows:

'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;
  }
};

I have a helper function for creating multiple users for the purpose of testing that looks something like this:

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 });

The output of the logging is:

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 *;

You'll see that brand_id is completely ignored in the query, even if I change the following:

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

Any idea what could be wrong?

It turns out I did not add the two new columns to the user model:

'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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM