在 Sequelize 3.x.x 版上,应用程序运行完美,没有任何问题。我尝试将其更新到版本 4.x.x 并修复了所有编译问题。但是,在某些情况下会抛出 Sequelize 错误,例如 instance.getGroup is not a function
。但是,IDE 确实允许我预先填充实例上的方法,但是在自动完成后,它会将其填充为 instance.getGroup
我在下面包含了一些我认为对这种情况有用的代码。
export interface IAccountInstance extends Sequelize.Instance<IAccountAttributes>, IAccountAttributes {
/**
* Gets all the Team objects associated with this instance.
*/
getGroup: Sequelize.BelongsToManyGetAssociationsMixin<IGroupInstance>;
}
如何调用 getGroups:当 GraphQL 请求存在于
groups
对象上的 account
属性时,它将使用 instance.getGroup()
解析(这是它在 v4 上中断的地方,以及许多其他像这样完成的方法)groups: {
type: new GraphQL.GraphQLList(SchemaRegistrar.schemaMap.groups.graphqlSchema),
resolve: (instance: IAccountInstance, args: any, context: any): any => {
if (!instance) {
return null;
}
return instance.getGroup();
}
},
从 https://github.com/sequelize/sequelize/blob/v4/docs/upgrade-to-v4.md ,我知道有重大变化,但我很幸运。
模式如何初始化,在服务器启动时,会调用一个操作来创建所有表和序列化模型。有更多的代码,但这通常是如何完成的
const schemaModel: Sequelize.Model<any, any> = ServerGlobals.sequelize.define(
schemaDef.tableName, schemaDef.sequelizeSchema, {
instanceMethods: {
schemaName: schemaDef.schemaName
},
freezeTableName: true
});
Sequelize 提到这已经改变了(这是我的代码中需要更新的内容?...)
const Model = sequelize.define('Model', {
...
}, {
classMethods: {
associate: function (model) {...}
},
instanceMethods: {
someMethod: function () { ...}
}
});
至
const Model = sequelize.define('Model', {
...
});
// Class Method
Model.associate = function (models) {
...associate the models
};
// Instance Method
Model.prototype.someMethod = function () {..}
但是 Model.associate 对我不起作用,我不确定如何让它与我的设置一起工作......
任何帮助表示赞赏。我已经通过其他人遇到了这个问题,但无法根据他们的解决方案解决它。
更新 1
堆栈跟踪中的确切错误是
error: instance.getGroup is not a function TypeError: instance.getGroup is not a function
关系就是这样建立起来的。表模式初始化后,执行以下操作
_.forEach(
this._sortedSchemas, (schema: AbstractSchemaDefinition<any, any>) =>
schema.createSequelizeRelationships(SchemaRegistrar.schemaMap)
);
createSequelizeRelationships
是一个不返回任何内容的函数,但具有与另一个表的关系属性,例如:schemaMap.accounts.sequelizeModel.hasMany(
schemaMap.groups.sequelizeModel,
{
as: groups,
foreignKey: accountId
}
);
最佳答案
所以这将是我拥有的新 Sequelize 项目中一个简单对象的典型设置......我花了一段时间才让关联起作用
这将在一个名为 loc_countries.js 的文件中,并在第二个片段的代码中导入
module.exports = function (sequelize, DataTypes) {
const Country = sequelize.define('loc_countries', {
id: {
type: DataTypes.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: true,
},
abbr2: {
type: DataTypes.STRING,
allowNull: true,
},
abbr3: {
type: DataTypes.STRING,
allowNull: true,
},
}, {
freezeTableName: true,
timestamps: false,
tableName: 'loc_countries',
});
Country.associate = function (models) {
models.Country.hasMany(models.ProvinceState, { as: 'ProvStates', foreignKey: 'loc_countries_id' });
};
return Country;
};
然后在我的 index.js 中,我通过您描述的 forEach 运行关联。
const models = {
Country: sequelize.import('./loc_countries'),
ProvinceState: sequelize.import('./loc_state_prov'),
City: sequelize.import('./loc_inc_places'),
};
Object.keys(models).forEach((modelName) => {
if ('associate' in models[modelName]) {
// console.log(models[modelName]);
models[modelName].associate(models);
}
});
关于node.js - Sequelize : instance. get 不是函数(升级后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55573837/