Skip to content

Commit e980ea5

Browse files
committed
Merge pull request dresende#122 from dresende/has_one_fix
hasOne reverse model should not have association field
2 parents 2796fa3 + e66bfa4 commit e980ea5

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

lib/Associations/One.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ exports.prepare = function (Model, associations, association_properties, model_f
2424
};
2525
associations.push(association);
2626
association_properties.push(association.field);
27-
model_fields.push(association.field);
27+
if(!opts.reversed) {
28+
model_fields.push(association.field);
29+
}
2830

2931
if (opts.reverse) {
3032
OtherModel.hasOne(opts.reverse, Model, {

test/integration/test-association-hasone-reverse.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,39 @@ var common = require('../common');
22
var assert = require('assert');
33

44
common.createConnection(function (err, db) {
5-
common.createModel2Table('test_association_hasone_reverse', db.driver.db, function () {
6-
common.insertModel2Data('test_association_hasone_reverse', db.driver.db, [
7-
{ id : 1, name : 'test1', assoc: 2 },
8-
{ id : 2, name : 'test2', assoc: 0 }
9-
], function (err) {
10-
if (err) throw err;
5+
common.createModelTable('test_association_hasone_reverse_parent', db.driver.db, function() {
6+
common.createModel2Table('test_association_hasone_reverse_child', db.driver.db, function () {
7+
common.insertModelData('test_association_hasone_reverse_parent', db.driver.db, [
8+
{ id : 1, name : 'parent 1' },
9+
{ id : 2, name : 'parent 2' }
10+
], function (err) {
11+
if (err) throw err;
12+
common.insertModel2Data('test_association_hasone_reverse_child', db.driver.db, [
13+
{ id : 1, name : 'child 1', assoc: 2 },
14+
{ id : 2, name : 'child 2', assoc: 1 }
15+
], function (err) {
16+
if (err) throw err;
1117

12-
var TestModel = db.define('test_association_hasone_reverse', common.getModelProperties());
13-
TestModel.hasOne("assoc", TestModel, { reverse: "reverseassoc" });
18+
var TestModelParent = db.define('test_association_hasone_reverse_parent', common.getModelProperties());
19+
var TestModelChild = db.define('test_association_hasone_reverse_child', common.getModelProperties());
20+
TestModelChild.hasOne("assoc", TestModelParent, { reverse: "reverseassoc" });
1421

15-
TestModel(2).getReverseassoc(function (err, Tests) {
16-
assert.equal(err, null);
17-
assert.equal(Array.isArray(Tests), true);
18-
assert.equal(typeof Tests[0], "object");
19-
assert.equal(Tests[0].id, 1);
20-
db.close();
22+
TestModelParent(2).getReverseassoc(function (err, children) {
23+
assert.equal(err, null);
24+
assert.equal(Array.isArray(children), true);
25+
assert.equal(typeof children[0], "object");
26+
assert.equal(children[0].id, 1);
27+
28+
// Make sure the association field hasn't been erroneously added to the reverse association model.
29+
TestModelParent.find({}, function (err, parents) {
30+
assert.equal(err, null);
31+
assert.equal(parents[0].hasOwnProperty('name'), true);
32+
assert.equal(parents[0].hasOwnProperty('assoc_id'), false);
33+
34+
db.close();
35+
});
36+
});
37+
});
2138
});
2239
});
2340
});

0 commit comments

Comments
 (0)