Sails.js 如何将数据插入联接 table(多对多)
Sails.js How to insert data into a join table (Many to Many)
我在将数据插入到我的连接中时出错 table,我不知道我的操作是否正确。
这是我的 2 个具有多对多关联的模型。
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'
}
}
};
当我尝试以这种方式将问题插入到提交中时:
Commit.create(commit).exec(function(err,created){
if (err) {
console.log(err);
}
else {
created.issues.add(issues);
created.save(function(err) {});
}
});
我的提交已创建,我的问题已创建,但它们之间没有 link 任何东西,连接 table 保持为空。我哪里弄错了?
从您的代码看来,您似乎在尝试添加一系列问题,尝试单独进行关联,就像他们在 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) {});
}
});
我在将数据插入到我的连接中时出错 table,我不知道我的操作是否正确。 这是我的 2 个具有多对多关联的模型。
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'
}
}
};
当我尝试以这种方式将问题插入到提交中时:
Commit.create(commit).exec(function(err,created){
if (err) {
console.log(err);
}
else {
created.issues.add(issues);
created.save(function(err) {});
}
});
我的提交已创建,我的问题已创建,但它们之间没有 link 任何东西,连接 table 保持为空。我哪里弄错了?
从您的代码看来,您似乎在尝试添加一系列问题,尝试单独进行关联,就像他们在 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) {});
}
});