我使用帆0.12.3和的PostgreSQL在Mac OSX 10.3.4(10.10.5),和我試圖實現through association:如何創建關聯?
我已經建立了兩個模型用第三個作爲連接表。當我嘗試測試通過創建一個關係,它是所有的設置我得到這個錯誤:
Uncaught TypeError: Cannot read property 'via' of undefined
這裏是我的測試(摩卡/柴) - 我認爲錯誤實際上是在該方法創建關聯(因爲如果我創建模型的加入作爲一個單獨的稱之爲作品:
describe('with orgs', function() {
beforeEach(function(done) {
var testEmail = '[email protected]';
var orgName = 'Awesome Org';
Org.create({name:orgName})
.then(function(org) {
return User.create({username:testEmail, orgs:[org]})
})
.then(function(user) {
done()
})
.catch(done);
});
it('populates empty list of orgs', function(done) {
console.log('about to call find');
User.find().populate('orgs')
.then(function(users) {
console.log('users', users);
var user = users[0]
assert.isNotNull(user.orgs);
assert.equal(user.orgs.length, 1);
done();
})
.catch(done);
});
});
這裏是我的模型:
user.js的:
module.exports = {
attributes: {
username : { type: 'email' },
orgs: {
collection: 'org',
via: 'user',
through: 'orgmembership'
}
}
};
Org.js:
module.exports = {
attributes: {
name : 'string',
users: {
collection: 'user',
via: 'org',
through: 'orgmembership'
}
},
};
OrgMembership.js:
module.exports = {
tableName: 'org_membership',
attributes: {
user: { model: 'User', columnName: 'user_id', foreignKey: true},
org: { model: 'Org', columnName: 'org_id', foreignKey: true}
}
};
我已爲整個項目上github。
我發現這個other stackoverflow question,這似乎是做同樣的事情,並試圖遵循相同的模式,但它不適合我。
我會很感激任何指針或建議。謝謝!
看起來這是水線的錯誤,並已作爲修補[email protected]。我測試了您的項目,並按預期工作。 – particlebanana