Sequelize 保存多对多 [英] Sequelize saving many to many

查看:69
本文介绍了Sequelize 保存多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在徘徊是否有任何关于如何保存多对多关系的扩展教程?我发现文档不仅仅是基本的.它缺少许多用例示例.

I was wandering if there are any extended tutorials on how to save a many to many relationship? I find the documentation more than basic. Its missing many use case examples.

我有两个模型:客户端和规则.他们有 n:n 关系.

I have two models: Client and Rule. They have the n:n relationship.

客户:

var Client = sequelize.define('client', {
    title: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    company: {
        type: DataTypes.STRING(200),
        allowNull: false
    },
    vendor: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    consumer: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    address_id: {
      type: DataTypes.INTEGER,
      allowNull: true
    }
  },{
    paranoid: true,
    underscored: true,
    classMethods: {
      associate:function(models){
          Client.hasMany(models.rule, { through: 'client_rules', onDelete: 'cascade'});
      }
    }
  });

规则:

var Rule = sequelize.define('rule', {

    service_id: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    is_allowed: {
      type: DataTypes.BOOLEAN,
      defaultValue: false
    },
    valid_until: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    rule: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    type: {
      type: DataTypes.INTEGER, // 1 for company rule, 2 for individual rule
      allowNull: false, 
    },
    active: {
      type: DataTypes.BOOLEAN,
      defaultValue: true
    }

  },{
    underscored: true,
    paranoid: true,
    classMethods: {
      associate:function(models){
          Rule.belongsToMany(models.client, { through: 'client_rules', onDelete: 'cascade'});
          Rule.belongsTo(models.service, { foreignKey: 'service_id' } );

      }
    }
  });

现在我想为客户端创建一个新规则.所以我必须先创建规则,然后通过client_rules"将其关联到客户端.

Now I would like to create a new rule for client. So I would have to create the rule first and associate it then to the client through 'client_rules'.

我如何用 sequelize 做到这一点?这不起作用:

How do I do that with sequelize? This doesn't work:

var clientID = req.user.client_id;
Client.find({ id: clientID })
.then(function(client){
  return client.addRule(req.body)
})
.catch(function(err){
  console.log(err)
})

[TypeError: Cannot read property 'replace' of undefined]

推荐答案

好的,我找到了方法.文档非常混乱.

Ok I found out how to do it. The docs are very confusing.

    var clientID = req.user.client_id;
    return Rule.create(req.body)
    .then(function(newRule){
          var ruleToAdd = newRule;
          return Client.findOne({ where: { id: clientID } })
    .then(function(client){
            return client.addRule(ruleToAdd)
            .then(function(ans){
              return ruleToAdd;
            })
    })

这篇关于Sequelize 保存多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆