Sequelize()不返回sequelize的实例

时间:2018-10-31 07:52:09

标签: node.js postgresql sequelize.js sequelize-auto

我最近开始使用Sequelize来获取我的postgresql数据库的模型。 要映射数据库,我正在使用sequelize-auto。 当我以这种方式将参数发送给它的构造函数时,我能够使用sequelize-auto为数据库创建一个自动生成的映射:

init.js

let sequelizeAutoInstance = new SequelizeAuto(dbName,username,password,options)

但是当我尝试以这种方式发送Sequelize的实例时,它不起作用:

new-init.js

let sequelizeInstance = new Sequelize(sequelizeOptions);
sequelizeAutoInstance = new SequelizeAuto(sequelizeInstance)

看看sequelize-auto的故事,我发现它行如下:

if (database instanceof Sequelize) {
    this.sequelize = database;
}

但是返回新Sequelize形式的实例不会返回Sequelize实例。

我想念什么? 谢谢

1 个答案:

答案 0 :(得分:1)

如果我告诉您正确的话。您需要将sequelize实例传递给模型。如果使用扩展模型,则只需将实例传递到init选项中即可。

const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = require("../your/db/file");

class User extends Model {}

User.init({
  // Model attributes are defined here
  firstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  lastName: {
    type: DataTypes.STRING
    // allowNull defaults to true
  }
}, {
  // Other model options go here
  sequelize, // We need to pass the connection instance
  modelName: 'User' // We need to choose the model name
});

如果功能正常

const { Sequelize, DataTypes } = require('sequelize');
const sequelize = require("../your/db/file");

const User = sequelize.define('User', {
  // Model attributes are defined here
  firstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  lastName: {
    type: DataTypes.STRING
    // allowNull defaults to true
  }
}, {
  // Other model options go here
});

所有这些都可以在Sequelize models文档中找到

相关问题