Skip to content

Commit 6a74796

Browse files
committed
Fix for smart_associations
Fixes an issue with generating the names and determining the types of fields on target models for hasOne associations. Handle `id: [...]` correctly by transferring it to keys instead of just blindly wrapping everything in an array. Also increased the timeout for hasMany associations tests, since they seemed to intermittently timeout.
1 parent 24c3db6 commit 6a74796

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

lib/Associations/One.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports.prepare = function (Model, associations, association_properties, model_f
3535
assocName = ucfirst(association.name);
3636

3737
if (!association.hasOwnProperty("field")) {
38-
association.field = util.formatField(Model, association.name, association.required, association.reversed);
38+
association.field = util.formatField(association.model, association.name, association.required, association.reversed);
3939
} else if(!association.extension) {
4040
association.field = util.wrapFieldObject(association.field, Model, Model.table, Model.properties);
4141
}

lib/Model.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function Model(opts) {
2525
opts = opts || {};
2626

2727
if ((!Array.isArray(opts.keys) || opts.keys.length < 2) && !opts.extension) {
28-
opts.keys = [ opts.id ];
28+
if (Array.isArray(opts.id)) {
29+
opts.keys = opts.id;
30+
} else {
31+
opts.keys = [opts.id];
32+
}
2933
} else {
3034
opts.id = null;
3135
}

lib/Utilities.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ exports.wrapFieldObject = function (obj, model, altName, alternatives) {
112112
exports.formatField = function (model, name, required, reversed) {
113113
var fields = {};
114114
var keys;
115-
if (model.keys instanceof Array) {
115+
if (Array.isArray(model.keys)) {
116116
keys = model.keys;
117117
}
118118
else {
@@ -129,8 +129,8 @@ exports.formatField = function (model, name, required, reversed) {
129129
required: required
130130
};
131131

132-
if (model.properties.hasOwnProperty(model.keys[i])) {
133-
var p = model.properties[model.keys[i]];
132+
if (model.properties.hasOwnProperty(keys[i])) {
133+
var p = model.properties[keys[i]];
134134
fieldOpts = {
135135
type: p.type || "number",
136136
size: p.size || 4,

test/integration/association-hasmany.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var should = require('should');
22
var helper = require('../support/spec_helper');
33
var ORM = require('../../');
44

5-
describe("hasMany", function() {
5+
describe("hasMany", function () {
6+
this.timeout(4000);
67
var db = null;
78
var Person = null;
89
var Pet = null;

0 commit comments

Comments
 (0)