2015-09-23 106 views
0

我正在使用Sequelize.js创建一些表。这些模型是用户,商店,角色和ShopUserRoles。在模型之间创建关联

我已经宣布在models/文件夹喜欢的机型:

user.js的

module.exports = function (sequelize, DataTypes) { 
    var User = sequelize.define('User', { 
     id : { 
      type: DataTypes.STRING, 
      unique: true, 
      primaryKey : true 
     }, 
     fullName : { 
      type: DataTypes.STRING, 
      field: 'full_name', 
      allowNull : false 
     }, 
    }, { 
     tableName: 'users', 

     classMethods: { 
      associate: function(models) { 
       User.hasMany(models.ShopUserRoles, {foreignKey: 'user_id' }); 
      } 
     }, 
     timestamps: true, 
     createdAt : 'created_at', 
     updatedAt : 'updated_at' 
    }); 

    return User; 
}; 

Shop.js

module.exports = function(sequelize, DataTypes) { 
    var Shop = sequelize.define('Shop', { 
     id : { 
      type : DataTypes.INTEGER, 
      unique : true, 
      primaryKey : true 
     }, 
     shopName : { 
      type : DataTypes.STRING, 
      field : 'shop_name', 
      allowNull : false 
     }, 
     status : { 
      type: DataTypes.STRING 
     } 
    }, { 
     tableName : 'shops', 

     classMethods : { 
      associate : function(models) { 
       Shop.hasMany(models.ShopUserRoles, {foreignKey : 'shop_id'}); 
      } 
     }, 
     timestamps : true, 
     createdAt : 'created_at', 
     updatedAt: 'updated_at' 
    }); 

    return Shop; 
}; 

Role.js

module.exports = function(sequelize, DataTypes){ 
    var Role = sequelize.define('Role', { 
     id : { 
      type: DataTypes.INTEGER, 
      unique: true, 
      primaryKey: true 
     }, 
     name : { 
      type: DataTypes.STRING, 
      unique: true 
     } 
    }, { 
     tableName : 'roles', 

     classMethods : { 
      associate : function(models) { 
       Role.hasMany(models.ShopUserRoles, {foreignKey: 'role_id'}); 
      } 
     }, 
     timestamps : true, 
     createdAt : 'created_at', 
     updatedAt: 'updated_at' 
    }); 

    return Role; 
}; 

ShopU serRoles.js

module.exports = function(sequelize, DataTypes) { 
    var ShopUserRoles = sequelize.define('ShopUserRoles', { 
    }, { 
     tableName : 'shop_user_roles', 

     classMethods: { 
      associate: function(models) { 
       ShopUserRoles.belongsTo(models.User, {foreignKey: 'user_id' }); 
       ShopUserRoles.belongsTo(models.Shop, {foreignKey : 'shop_id'}); 
       ShopUserRoles.belongsTo(models.Role, {foreignKey: 'role_id'}); 
      } 
     }, 
     timestamps : true, 
     createdAt : 'created_at', 
     updatedAt : 'updated_at' 
    }); 

    return ShopUserRoles; 
}; 

而且我使用sequelize['import'](FILENAME)格式导入他们并添加相应的关联。

现在,因为我们已经添加了关联,所以应该在生成前三个表之后创建表shop_user_roles,否则外键添加将不起作用。但是,我看到Sequelize正在试图在users表创建之前创建此表。为了解决这个问题,我有三种选择:

  • 只有在表已创建
  • 分配表创建一个明确的排序或让Sequelize“数字”的事情了智能
  • 试图做分配协会整个事情两次(这是笨拙的,我不希望它在开发代码)

至于前两个选项,我不完全确定如何做那些使用Sequelize.js。任何形式的参考或帮助将不胜感激。

我正在使用Sequelize.js v3.9。

回答

0

下面是我如何解决这个问题。不幸的是,这是一次暴力尝试。

在我的应用程序中,我试图导出包含模型名称的数组,同时保持正确的顺序。虽然我们可以使用associate类方法获得模型之间的关联,但也可以在模型的classMethods属性内硬编码创建表的序列。对于User模型,这可能是像

classMethods: { 
    serial: 1, 
    associate: function(models) { 
     User.hasMany(models.ShopUserRoles, {foreignKey: 'user_id' }); 
    } 
} 

之后,基于包含这些型号名称可以做的伎俩阵列上的硬编码的串行一个简单的排序。

使用关联为了进行拓扑排序也是一个更好的方法。

相关问题