我正在使用Node,Feathers和Sequelize为SQL Server 2008数据库设置API。我已经使用feathers-sequelize-auto成功创建了模型和服务,并且大多数路线似乎都在起作用。该应用程序正在运行,但显示错误消息:
Unhandled Rejection at: Promise {"_bitField":18087936,"_fulfillmentHandler0":{}}
与其中一个外键有关的路线(/项目)之一出现错误。 / project的邮递员输出为:
{
"name": "GeneralError",
"message": "Invalid column name 'OrganisationID'.",
"code": 500,
"className": "general-error",
"data": {},
"errors": {}
}
所有操作都可以在数据库本身中正常进行,并且我可以在相关表上运行查询而没有问题。
Project.model.js的相关部分:
字段定义
LeadAgency: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'Organisation',
key: 'OrganisationID'
}
关系:
Project.associate = function(models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
Project.belongsTo(models.Team, { foreignKey: 'TeamID' });
Project.belongsTo(models.Subteam, { foreignKey: 'SubTeamID' });
Project.belongsTo(models.Staff, { foreignKey: 'StaffID' });
Project.belongsTo(models.Organisation, { foreignKey: 'OrganisationID' });
Project.belongsTo(models.Project, { foreignKey: 'ProjectID' });
};
这是Organisation.model.js代码:
/* jshint indent: 2 */
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
module.exports = function(app) {
const sequelizeClient = app.get('sequelizeClient');
const Organisation = sequelizeClient.define(
'Organisation',
{
OrganisationID: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
Name: {
type: DataTypes.STRING,
allowNull: false
},
Type: {
type: DataTypes.STRING,
allowNull: true
},
AddressID: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'Address',
key: 'AddressID'
}
}
},
{
tableName: 'Organisation',
timestamps: false
},
{
hooks: {
beforeCount(options) {
options.raw = true;
}
}
}
);
// eslint-disable-next-line no-unused-vars
Organisation.associate = function(models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
Organisation.belongsTo(models.Address, { foreignKey: 'AddressID' });
};
return Organisation;
};
Noob在这里可能会丢失一些明显的东西。帮助表示赞赏!
答案 0 :(得分:1)
由于model.associate中的映射不正确,导致出现此错误。在项目模型中,您还将Project
定义为关联者。这可能是原因。