From a73f53521ea6ea8a14ea640fca4dcf787f7a9684 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sat, 13 Apr 2013 05:35:53 +0400 Subject: [PATCH] Fix m2m: only add fk when hasMany have no "through" --- docs/model.md | 2 +- lib/relations.js | 7 +++++-- test/relations.test.js | 2 -- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/model.md b/docs/model.md index bfe08a70..c0ffeed6 100644 --- a/docs/model.md +++ b/docs/model.md @@ -181,7 +181,7 @@ TODO: document ### hasAndBelongsToMany -TODO: implement and document +TODO: document ## SEE ALSO diff --git a/lib/relations.js b/lib/relations.js index 3c665506..e1e76076 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -94,6 +94,7 @@ Model.hasMany = function hasMany(anotherClass, params) { d.destroy(done); }); }; + delete scopeMethods.destroy; } defineScope(this.prototype, params.through || anotherClass, methodName, function () { var filter = {}; @@ -106,8 +107,10 @@ Model.hasMany = function hasMany(anotherClass, params) { return filter; }, scopeMethods); - // obviously, anotherClass should have attribute called `fk` - anotherClass.schema.defineForeignKey(anotherClass.modelName, fk); + if (!params.through) { + // obviously, anotherClass should have attribute called `fk` + anotherClass.schema.defineForeignKey(anotherClass.modelName, fk); + } function find(id, cb) { anotherClass.find(id, function (err, inst) { diff --git a/test/relations.test.js b/test/relations.test.js index a56941b3..816379c9 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -229,8 +229,6 @@ describe('relations', function() { }); }); - it('should allow to destroy instance and connection'); - }); });