SequelizeJS 关系,同模型
SequelizeJS relations, same model
我想创建一个如下所示的模型:
var category = db.define('category', {
name: {
type: Types.STRING,
},
position: {
type: Types.INTEGER
}
}, {
classMethods:{
associate: function (models) {
category.belongsTo(models.category);
category.hasMany(models.category);
}
}
});
它在我的数据库中有效,我在类别的 table 中有 "CategoryId",但是当我想在我的查询中包含类别模型时它不起作用,我遇到了一个问题:
Error: ER_NONUNIQ_TABLE: Not unique table/alias: 'category'
我的查询:
this.context.category.findAll({include: [{model: this.context.category}]});
你只需要反转关系的顺序 (belongsTo / hasMany)
var category = sequelize.define('category', {
name: {
type: Sequelize.STRING,
},
position: {
type: Sequelize.INTEGER
}
}, {
classMethods:{
associate: function (c) {
category.hasMany(c);
category.belongsTo(c);
}
}
});
我想创建一个如下所示的模型:
var category = db.define('category', {
name: {
type: Types.STRING,
},
position: {
type: Types.INTEGER
}
}, {
classMethods:{
associate: function (models) {
category.belongsTo(models.category);
category.hasMany(models.category);
}
}
});
它在我的数据库中有效,我在类别的 table 中有 "CategoryId",但是当我想在我的查询中包含类别模型时它不起作用,我遇到了一个问题:
Error: ER_NONUNIQ_TABLE: Not unique table/alias: 'category'
我的查询:
this.context.category.findAll({include: [{model: this.context.category}]});
你只需要反转关系的顺序 (belongsTo / hasMany)
var category = sequelize.define('category', {
name: {
type: Sequelize.STRING,
},
position: {
type: Sequelize.INTEGER
}
}, {
classMethods:{
associate: function (c) {
category.hasMany(c);
category.belongsTo(c);
}
}
});