SequelizeJS关系,同型号

时间:2015-02-11 08:25:58

标签: mysql node.js sequelize.js

我想创建一个如下所示的模型:

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);
      }
    }
  });

它在我的数据库中工作,我在类别表中有“CategoryId”但是当我想在我的查询中包含类别模型时它不起作用,我遇到了一个问题:

Error: ER_NONUNIQ_TABLE: Not unique table/alias: 'category'

我的疑问:

this.context.category.findAll({include: [{model: this.context.category}]});

1 个答案:

答案 0 :(得分:1)

你只需要提供关系的顺序(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);
      }
    }
  });