繁体   English   中英

用不是 Sequelize.Model 子类的东西调用的 belongsToMany 错误

[英]belongsToMany called with something that's not a subclass of Sequelize.Model error

你好我的地址 model

const { DataTypes } = require('sequelize');  
const sequelize = require('../../../db/connection');  
var BorrowerInfo = require('../../models/borrowerInfo/BorrowerInfo')
const Address = sequelize.define('addresses', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
    },
    street: {
        type: DataTypes.STRING,
        allowNull: false
    },
    unit: {
        type: DataTypes.STRING,
        allowNull: false
    },
    city: {
        type: DataTypes.STRING,
        allowNull: false
    },
    state: {
        type: DataTypes.STRING,
        allowNull: false
    },
    zip: {
        type: DataTypes.STRING,
        allowNull: false
    },
    country: {
        type: DataTypes.STRING,
        allowNull: false
    },
},
    {
        freezeTableName: true
    },
    {
    }
);
Address.associate = (models) => {
    Address.belongsToMany(BorrowerInfo, {
        through: 'BorrowerAddresses',
        as: 'address',
        foreignKey: 'addressId'
      });
  };
module.exports = Address;

我的借款人模型

 const { DataTypes } = require('sequelize');
const sequelize = require('../../../db/connection');

const Address = require('../../models/common/Address')


const BorrowerInfo = sequelize.define('borrower_info', {
    id : {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true ,
        allowNull: false,
    },
    loanAppNo : {
        type: DataTypes.STRING,
        allowNull: false
    },
    firstName: {
        type: DataTypes.STRING,
        allowNull: false
    },
    lastName: {
        type: DataTypes.STRING,
        allowNull: false
    },
    middleName:{
        type: DataTypes.STRING,
        allowNull: false
    },
    suffix: {
        type: DataTypes.STRING,
        allowNull: false
    },
    ssn: {
        type: DataTypes.STRING,
        allowNull: false
    },
    dob: {
        type: DataTypes.DATE,
        allowNull: false
    },
    citizenship: {
        type: DataTypes.STRING,
        allowNull: false
    },
    maritalStatus: {
        type: DataTypes.ENUM('1','2','3'),
    },
    noOfDependantChilds: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    agesDependantChilds: {
        type: DataTypes.STRING,
        allowNull: false
    },
    contactInfo: {
        type: DataTypes.JSON,
        allowNull: false
    }, 
    createdAt: {
        type: DataTypes.NOW,
    }
},
    { 
        timestamps: false,
        freezeTableName: true
    },
    {
    }
);

    BorrowerInfo.belongsToMany(Address, {
        through: 'borrower_addresses',
      });
module.exports = BorrowerInfo;  

我的借款人地址 model

const {
    DataTypes
} = require('sequelize');
var Addresses = require('../common/Address')
const sequelize = require('../../../db/connection');

const BorrowerAddresses = sequelize.define('borrower_addresses', {

id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false,
},
borrowerInfoId: {
    type: DataTypes.INTEGER,
    allowNull: false
},
type: {
    type: DataTypes.ENUM('current', 'former', 'mailing'),
    allowNull: false
},
addressId: {
    type: DataTypes.INTEGER,
    allowNull: false
},
howLongAtCurrentAddress: {
    type: DataTypes.INTEGER,
    allowNull: false
},
housing: {
    type: DataTypes.ENUM('1', '2', '3'),
},
rentPerMonth: {
    type: DataTypes.FLOAT,
    allowNull: false
},
createdAt: {
    type: DataTypes.NOW,
}
}, {
    timestamps: false,
    freezeTableName: true
});

module.exports = BorrowerAddresses;  

错误:borrower_info.belongsToMany 调用的东西不是 Sequelize.Model 的子类

当我在借款人信息 model 内安慰地址 model 时
BorrowerInfo:class 扩展 Model {} 地址:{}

地址 model 未扩展 model class

but when i am consoling borrower info model inside address model it is extending the model class what's the issue please guide how can i get rid of this error

请标记一些可以提供帮助的人

您的 model 定义是实际的 model 名称。 因此,使 model 名称与下面的定义相同。

module.exports = (sequelize, DataTypes) => {
  const BorrowerInfo = sequelize.define('BorrowerInfo', {
     id : {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true ,
    allowNull: false,
    },
    ....
   {
    tableName: 'borrower_info', // you can pass your tablename here
  });
  return BorrowerInfo;
};

如果您的 model 名称是Address ,则后续会将 map 这个 model 传递给一个名为Addresses的表,您可以通过显式传递表名来防止这种行为。

如果有人卡在这里,我找到了解决方案

实际上,我必须在单个文件中初始化所有模型,而不是从单个文件中导入它,例如。

文件:index.js

'use strict';
const fs = require('fs');
const path = require('path');
const basename = path.basename(module.filename);
const sequelize = require('../../db/connection');

let db = {};
// borrower info
fs.readdirSync(`${__dirname}/borrowerInfo`)
    .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file !== basename);
    })
    .forEach(function (file) {
        if (file.slice(-3) !== '.js') return;
        let model = sequelize['import'](path.join(`${__dirname}/borrowerInfo`, file));
        db[model.name] = model;
    });


//loan team
fs.readdirSync(`${__dirname}/loanTeam`)
    .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file !== basename);
    })
    .forEach(function (file) {
        if (file.slice(-3) !== '.js') return;
        let model = sequelize['import'](path.join(`${__dirname}/loanTeam`, file));
        db[model.name] = model;
    });

// common
fs.readdirSync(`${__dirname}/common`)
    .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file !== basename);
    })
    .forEach(function (file) {
        if (file.slice(-3) !== '.js') return;
        let model = sequelize['import'](path.join(`${__dirname}/common`, file));
        db[model.name] = model;
    });


// role
fs.readdirSync(`${__dirname}/role`)
    .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file !== basename);
    })
    .forEach(function (file) {
        if (file.slice(-3) !== '.js') return;
        let model = sequelize['import'](path.join(`${__dirname}/role`, file));
        db[model.name] = model;
    });

// user
fs.readdirSync(`${__dirname}/user`)
    .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file !== basename);
    })
    .forEach(function (file) {
        if (file.slice(-3) !== '.js') return;
        let model = sequelize['import'](path.join(`${__dirname}/user`, file));
        db[model.name] = model;
    });

Object.keys(db).forEach(function (modelName) {
    if (db[modelName].associate) {
        db[modelName].associate(db);
    }
});


db.sequelize = sequelize;
module.exports = db;   

在其他文件中导入

const models = require('../models/index')
const exampleTable = models.tableName

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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