nearinfinity/node-persist — pull request #76 by CJNE (2013-05-30 21:51:29+00:00)
Title: Fix so that multiple hasOne with the same model can be included

This fixes (in a crude way) a problem that occurs when you have multiple hasOne relations that references the same model. For example:

module.exports = Calibration = persist.define("Calibration", {
"created_at": { type: type.DATETIME, defaultValue: function() { return new Date() } },
"m0": { type: type.INTEGER },
"m1": { type: type.INTEGER },
"m2": { type: type.INTEGER },
"m3": { type: type.INTEGER }
})
.hasOne(Measurement, { foreignKey: 'm0', name: "vol0", createHasMany: false })
.hasOne(Measurement, { foreignKey: 'm1', name: "vol1", createHasMany: false })
.hasOne(Measurement, { foreignKey: 'm2', name: "vol2", createHasMany: false })
.hasOne(Measurement, { foreignKey: 'm3', name: "vol3", createHasMany: false });

Calibration.orderBy('id').limit(5, 0).include(['vol0', 'vol1', 'vol2', 'vol3']).all(connection, function(err, calibrations) { }

would return measurement model vol3 for vol0, vol1, and vol2.

The fix in this pull request is not ideal (it only works if the columns are ordered and named the same way) but it fixes the problem.
It could perhaps help finding a proper solution to this, i would guess the problem would be better fixed in the groupBy function.