2016-06-12 30 views
2

私は、Mac OSX(10.10.5)に帆0.12.3のPostgreSQL 9.3.4を使用している、と私は、私は2つのモデルを設定したthrough associationSailsJSによる関連付け:関連付けの作成方法?

を実装しようとしていますジョインテーブルとして3番目私はそれがすべての関係を作成することによって設定されていることをテストしようとしたとき、私はこのエラーを取得:

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にプロジェクト全体を掲載しました。

これは同じことをしているように見えますが、同じパターンに従おうとしましたが、私のためには機能しません。

私は指針や提案をいただければ幸いです。ありがとうございました!

+1

に適用され、これはウォーターラインのバグだったとして、パッチが適用されているように見えます[email protected]。あなたのプロジェクトでテストしたところ、期待どおりに動作しました。 – particlebanana

答えて

-1

Users.jsはOrg.jsで「経由」の定義における「ユーザー」を参照してください、同じことがOrgMembership.js

module.exports = { 
    attributes: { 
    username : { type: 'email' }, 
    orgs: { 
     collection: 'org', 
     via: 'users', 
     through: 'orgmembership' 
    } 
    } 
}; 
+0

deisgnは、すべての組織単位に1人のユーザーと1つの組織が必要であり、OrgMembershipで複数であることがわかりません。私はそれをどうにかしてみましたが、それは私にとってはうまくいかなかった - テストはまだ失敗します。 – Ultrasaurus

+0

ここにあなたの提案に基づいて試した変更があります:https://github.com/ultrasaurus/sails-many-to-many/commit/2b65e1f294da617693a670541c704a4a04aed4bf – Ultrasaurus

+0

セイルズはコレクションを参照しようとしています。それはあなたの物理的なファイルです"org.js"、 "user.js"などを作成したので、 "collection"の値はそのファイルに与えられた名前と一致しなければなりません。 「ビア」割り当ては、関連を示す属性であり、モデル内で明示的に定義されたフィールドとも一致しなければならない。あなたのご意見ありがとうございます。 – Luke

関連する問題