Sails.js如何将数据插入联接表(多对多) [英] Sails.js How to insert data into a join table (Many to Many)

查看:228
本文介绍了Sails.js如何将数据插入联接表(多对多)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将数据插入到联接表中时出现错误,我不知道自己是否以正确的方式进行操作. 这是我的2个具有多对多关联的模型.

I am having an error inserting data into my join table and I don't know if I'm doing it the right way. Here are my 2 models that have the many to many association.

Commit.js:

Commit.js:

module.exports = {
  schema: true,
  attributes: {
    idCommit : {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true,
      unique: true
    },
    revision : {
    type: 'integer',
    required: true
    },
    issues: {
      collection: 'issue',
      via: 'commits',
      dominant: true
    }
  }
};

Issue.js:

module.exports = {
  schema: true,
  attributes: {
    idIssue : {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true,
      unique: true
    },
    description: {
    type: 'string',
    required: true
    },
    commits: {
      collection: 'commit',
      via: 'issues'
    }
  }
};

当我尝试通过这种方式将问题插入到提交中时:

When I try to insert issues into a commit this way :

Commit.create(commit).exec(function(err,created){
  if (err) {
    console.log(err);
  }
  else { 
    created.issues.add(issues);
    created.save(function(err) {});
  }
});

创建了我的提交,创建了我的问题,但是它们之间没有任何链接,联接表仍然为空.我在哪里弄错了?

My commit is created, my issues are created, but there is no link what so ever between them and the join table stays empty. Where did I get it wrong?

推荐答案

从您的代码中看来,您就像尝试添加一系列问题,尝试单独进行关联,就像在

From your code it looks like your trying to add an array of issues, try doing the association individually, like they do in the Many-to-Many docs

Commit.create(commit).exec(function(err,created){
  if (err) {
    console.log(err);
  }
  else { 

    issues.forEach(function(issue, index){
        created.issues.add(issue);
    })

    created.save(function(err) {});
  }
});

这篇关于Sails.js如何将数据插入联接表(多对多)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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