node.js 如何使用 sequelize 迁移/种子插入初始数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33323699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:25:02  来源:igfitidea点击:

How to insert initial data using sequelize migrations/seeds?

node.jsexpressmigrationsequelize.js

提问by nachocab

I'm trying to create my initial migration to populate the test database but I can't get it working. This is what I have in my migration:

我正在尝试创建我的初始迁移来填充测试数据库,但我无法让它工作。这是我在迁移中所拥有的:

'use strict';

module.exports = {
  up: function (queryInterface, Sequelize) {
    return [
      queryInterface.bulkInsert('Users', [
        { username: "user1" },
        { username: "user2" }
    ])];
  },

  down: function (queryInterface, Sequelize) {
    return queryInterface.dropTable('Users');
  }
};

And I get this error:

我得到这个错误:

== 20151024144833-create-conjugation: migrating =======
{ [SequelizeUniqueConstraintError: Validation error]
  name: 'SequelizeUniqueConstraintError',
  message: 'Validation error',
  errors: [],
  fields: [] }

There must be an easier way to do this. I've checked other SO questions, but the syntax has changed in the current version of sequelize.

必须有一种更简单的方法来做到这一点。我已经检查了其他 SO 问题,但是当前版本的 sequelize 中的语法已更改。

UPDATE

更新

Ok, I realized my mistake: I was assuming that sequelize would take care of the timestamps. This fixes the problem:

好吧,我意识到我的错误:我假设 sequelize 会处理时间戳。这解决了这个问题:

up: function (queryInterface, Sequelize) {
  console.log(User);
  return [
    queryInterface.bulkInsert('Users', [
      { username: "user1", createdAt: Date.now(), updatedAt: Date.now() },
      { username: "user2", createdAt: Date.now(), updatedAt: Date.now() }
    ])
  ];
}

But I'm still wondering if this is the right way to seed my database. Is there a way to do it using User.create({})?

但我仍然想知道这是否是我的数据库播种的正确方法。有没有办法做到这一点User.create({})

回答by cshion

An alternative could be use : sequelize fixtures, you could init your tables with default data declared as a json file or other format.

可以使用替代方法:sequelize fixtures,您可以使用声明为 json 文件或其他格式的默认数据来初始化您的表。

回答by Eduardo Cuomo

You can use next:

您可以使用下一个:

const City = sequelize.define('city', {
  name: { type: Sequelize.STRING },
  order_: { type: Sequelize.INTEGER }
});
City.sync().then(() => {
  City.create({
    name: 'Neuquen',
    order_: 0
  });
  City.create({
    name: 'General Roca',
    order_: 1
  });
});

Or read about "migrations" at http://docs.sequelizejs.com/en/latest/docs/migrations/

或者在http://docs.sequelizejs.com/en/latest/docs/migrations/阅读有关“迁移”的信息

回答by tjp

new Date()

also required for mysql, i.e.

mysql 也需要,即

  return queryInterface.bulkInsert('users', [
  {
    "Forename":"A",
    "Surname": "User",
    "UserType":"1",
    "Email":"[email protected]",
    "Password":"password",
    "LastLogin":0,
    "Tokens": JSON.stringify({"tokens":[]}),
    "CreatedBy": 999,
    "CreatedAt": new Date(),
    "UpdatedAt": new Date()
  }]);