feathersjs/sequelizejs -> MODEL 与 MODEL 没有关联
feathersjs/sequelizejs -> MODEL is not associated with MODEL
我在'services'文件夹下有两个文件夹。他们正在单独调用该服务。现在我想扩展一个模型以包括相关模型,这给出了一个错误信息:类型错误:无法读取未定义的属性'Model'
数据库存在,sql服务器,关系也在那里。
文件services\mic\activity\index.js
'use strict';
const service = require('feathers-sequelize');
const activity = require('./activity-model');
const hooks = require('./hooks/index');
module.exports = function () {
const app = this;
const options = {
Model: activity(app.get('mic_sequelize'))
};
app.use('/mic/activity', service(options));
const activityService = app.service('/mic/activity');
activityService.before(hooks.before);
activityService.after(hooks.after);
};
文件services\mic\activity\activity-model.js
'use strict';
const Sequelize = require('sequelize');
module.exports = function (sequelize) {
const activity = sequelize.define('activity', {
activity_pk: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
unique: true
},
activity_plan_pk: {
type: Sequelize.INTEGER,
allowNull: false,
unique: false
},
change_order_pk: {
type: Sequelize.INTEGER,
allowNull: true,
unique: false
},
description: {
type: Sequelize.STRING(255),
allowNull: false,
unique: false
},
id: {
type: Sequelize.STRING(255),
allowNull: false,
unique: true
}
}, {
freezeTableName: true,
paranoid: false,
timestamps : false,
underscored : false,
tableName: 'Activity',
classMethods: {
associate() {
activity.belongsTo(sequelize.models.activity_plan, {
allowNull: true,
as: 'activity_plan'
});
activity.belongsTo(sequelize.models.change_order, {
allowNull: false,
as: 'change_order'
});
}
}
});
return activity;
};
文件services\mic\activity\hooks\index.js
'use strict';
const globalHooks = require('../../../../hooks');
const hooks = require('feathers-hooks');
const auth = require('feathers-authentication').hooks;
exports.before = {
all: [],
find: [
getRelatedInfo()
],
get: [
getRelatedInfo()
],
create: [],
update: [],
patch: [],
remove: []
};
exports.after = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
};
function getRelatedInfo() {
return function (hook) {
hook.params.sequelize = {
attributes: [
'activity_pk',
'activity_plan_pk',
'change_order_pk',
['description', 'activity_description'],
['id', 'activity_id']
],
include: [
{
model: hook.app.services.activity_plan.Model,
as: 'activity_plan',
required: false,
attributes: [
['description', 'activity_plan_description'],
['id', 'activity_plan_id']
]
}
]
}
};
return Promise.resolve(hook);
}
文件services\mic\activity_plan\index.js
'use strict';
const service = require('feathers-sequelize');
const activity_plan = require('./activity_plan-model');
const hooks = require('./hooks/index');
module.exports = function () {
const app = this;
const options = {
Model: activity_plan(app.get('mic_sequelize'))
};
app.use('/mic/activity_plan', service(options));
const activityplanService = app.service('/mic/activity_plan');
activityplanService.before(hooks.before);
activityplanService.after(hooks.after);
};
文件services\mic\activity_plan\activity_plan-model.js
'use strict';
const Sequelize = require('sequelize');
module.exports = function (sequelize) {
const activity_plan = sequelize.define('activity_plan', {
activity_plan_pk: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
unique: true
},
description: {
type: Sequelize.STRING(255),
allowNull: false,
unique: false
},
id: {
type: Sequelize.STRING(255),
allowNull: false,
unique: true
}
}, {
freezeTableName: true,
paranoid: false,
timestamps : false,
underscored : false,
tableName: 'Activity_Plan',
classMethods: {
associate() {
activity_plan.hasMany(sequelize.models.activity, {
as: 'activity_plan',
foreignKey: 'activity_plan_pk',
targetKey: 'activity_plan_pk'
});
}
}
});
return activity_plan;
};
文件service\mic\activity_plan\hooks\index.js
'use strict';
const globalHooks = require('../../../../hooks');
const hooks = require('feathers-hooks');
const auth = require('feathers-authentication').hooks;
exports.before = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
};
exports.after = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
};
问题
我认为我的某个地方配置错误。我使用了我自己的一个工作示例应用程序,其中服务没有子文件夹并且关系是相同的。那是有效的。知道问题出在哪里吗?
求解
好的。因此,我将 activity 和 activity_Plan 移回服务的根目录,除了文件位置外,没有更改任何内容。同样的错误。然后我更改了服务的路径(删除了“/mic”),现在我得到 'activity_plan' is not associated with 'activity'.
好的,现在我恢复了我的测试并得到了这一行 model: hook.app.service('/mic/activity_plan').Model
。然后我想到了什么。我有自定义主键,所以我需要在 hasMany、belongsTo 部分做一些调查
所以现在我制作了第二个数据库,它是通过 sequelize(表)strangly 制作的,完全相同的问题。
如果我查看这一行 at Model.validateIncludedElement (D:\Workspace\Projects. Live Customers\Mexon Technology\Projects - Mexon\MexonInControl\MultiFunctionalPortal\backend\node_modules\sequelize\lib\model.js:558:11)
,然后在它指向的代码中查看,这很可能是我的模型命名与我的代码中的行 model: hook.app.service('/mfp/sys_metadata').Model
相结合的问题.
哦,亲爱的。我完全忘记了在服务子文件夹的 index.js 文件中初始化关联:
Object.keys(sequelize.models).forEach(function(modelName) {
if ("associate" in sequelize.models[modelName]) {
sequelize.models[modelName].associate();
}
});
sequelize.sync(
{
force: false
}
);
我在'services'文件夹下有两个文件夹。他们正在单独调用该服务。现在我想扩展一个模型以包括相关模型,这给出了一个错误信息:类型错误:无法读取未定义的属性'Model'
数据库存在,sql服务器,关系也在那里。
文件services\mic\activity\index.js
'use strict';
const service = require('feathers-sequelize');
const activity = require('./activity-model');
const hooks = require('./hooks/index');
module.exports = function () {
const app = this;
const options = {
Model: activity(app.get('mic_sequelize'))
};
app.use('/mic/activity', service(options));
const activityService = app.service('/mic/activity');
activityService.before(hooks.before);
activityService.after(hooks.after);
};
文件services\mic\activity\activity-model.js
'use strict';
const Sequelize = require('sequelize');
module.exports = function (sequelize) {
const activity = sequelize.define('activity', {
activity_pk: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
unique: true
},
activity_plan_pk: {
type: Sequelize.INTEGER,
allowNull: false,
unique: false
},
change_order_pk: {
type: Sequelize.INTEGER,
allowNull: true,
unique: false
},
description: {
type: Sequelize.STRING(255),
allowNull: false,
unique: false
},
id: {
type: Sequelize.STRING(255),
allowNull: false,
unique: true
}
}, {
freezeTableName: true,
paranoid: false,
timestamps : false,
underscored : false,
tableName: 'Activity',
classMethods: {
associate() {
activity.belongsTo(sequelize.models.activity_plan, {
allowNull: true,
as: 'activity_plan'
});
activity.belongsTo(sequelize.models.change_order, {
allowNull: false,
as: 'change_order'
});
}
}
});
return activity;
};
文件services\mic\activity\hooks\index.js
'use strict';
const globalHooks = require('../../../../hooks');
const hooks = require('feathers-hooks');
const auth = require('feathers-authentication').hooks;
exports.before = {
all: [],
find: [
getRelatedInfo()
],
get: [
getRelatedInfo()
],
create: [],
update: [],
patch: [],
remove: []
};
exports.after = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
};
function getRelatedInfo() {
return function (hook) {
hook.params.sequelize = {
attributes: [
'activity_pk',
'activity_plan_pk',
'change_order_pk',
['description', 'activity_description'],
['id', 'activity_id']
],
include: [
{
model: hook.app.services.activity_plan.Model,
as: 'activity_plan',
required: false,
attributes: [
['description', 'activity_plan_description'],
['id', 'activity_plan_id']
]
}
]
}
};
return Promise.resolve(hook);
}
文件services\mic\activity_plan\index.js
'use strict';
const service = require('feathers-sequelize');
const activity_plan = require('./activity_plan-model');
const hooks = require('./hooks/index');
module.exports = function () {
const app = this;
const options = {
Model: activity_plan(app.get('mic_sequelize'))
};
app.use('/mic/activity_plan', service(options));
const activityplanService = app.service('/mic/activity_plan');
activityplanService.before(hooks.before);
activityplanService.after(hooks.after);
};
文件services\mic\activity_plan\activity_plan-model.js
'use strict';
const Sequelize = require('sequelize');
module.exports = function (sequelize) {
const activity_plan = sequelize.define('activity_plan', {
activity_plan_pk: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
unique: true
},
description: {
type: Sequelize.STRING(255),
allowNull: false,
unique: false
},
id: {
type: Sequelize.STRING(255),
allowNull: false,
unique: true
}
}, {
freezeTableName: true,
paranoid: false,
timestamps : false,
underscored : false,
tableName: 'Activity_Plan',
classMethods: {
associate() {
activity_plan.hasMany(sequelize.models.activity, {
as: 'activity_plan',
foreignKey: 'activity_plan_pk',
targetKey: 'activity_plan_pk'
});
}
}
});
return activity_plan;
};
文件service\mic\activity_plan\hooks\index.js
'use strict';
const globalHooks = require('../../../../hooks');
const hooks = require('feathers-hooks');
const auth = require('feathers-authentication').hooks;
exports.before = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
};
exports.after = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
};
问题
我认为我的某个地方配置错误。我使用了我自己的一个工作示例应用程序,其中服务没有子文件夹并且关系是相同的。那是有效的。知道问题出在哪里吗?
求解
好的。因此,我将 activity 和 activity_Plan 移回服务的根目录,除了文件位置外,没有更改任何内容。同样的错误。然后我更改了服务的路径(删除了“/mic”),现在我得到 'activity_plan' is not associated with 'activity'.
好的,现在我恢复了我的测试并得到了这一行 model: hook.app.service('/mic/activity_plan').Model
。然后我想到了什么。我有自定义主键,所以我需要在 hasMany、belongsTo 部分做一些调查
所以现在我制作了第二个数据库,它是通过 sequelize(表)strangly 制作的,完全相同的问题。
如果我查看这一行 at Model.validateIncludedElement (D:\Workspace\Projects. Live Customers\Mexon Technology\Projects - Mexon\MexonInControl\MultiFunctionalPortal\backend\node_modules\sequelize\lib\model.js:558:11)
,然后在它指向的代码中查看,这很可能是我的模型命名与我的代码中的行 model: hook.app.service('/mfp/sys_metadata').Model
相结合的问题.
哦,亲爱的。我完全忘记了在服务子文件夹的 index.js 文件中初始化关联:
Object.keys(sequelize.models).forEach(function(modelName) {
if ("associate" in sequelize.models[modelName]) {
sequelize.models[modelName].associate();
}
});
sequelize.sync(
{
force: false
}
);