Open anongit opened 11 years ago
User = persist.define("User", { 'email': {'type': 'string', 'primaryKey': true}, 'name': 'string', }) .on("beforeSave", function(self) { // have to save and restore email, because // last_id is assigned to it after insert self._email = self.email; }) .on("afterSave", function(self) { self.email = self._email; delete self._email; }); var Comment = persist.define("Comment", { 'text': 'string', }) .hasOne(User, { 'foreignKey': 'user_email' });
The hack with save and restore email is for this line: https://github.com/nearinfinity/node-persist/blob/master/lib/connection.js#L115
When I try to select user.comments I get nothing, query is:
user.comments
select t0."text" AS c0, t0."id" AS c1, t0."user_email" AS c2 FROM Comments AS t0 WHERE user_id = ? LIMIT 1 [ 'user@example.com' ]
As you can see it uses user_id instead of user_email. I get the results if I delete this line: https://github.com/nearinfinity/node-persist/blob/master/lib/model.js#L334
What reasons do you have to delete foreignKey when you make reversed association in parent model? If child.key references parent.id, then parent.id must reference child.key, not child.id.
The hack with save and restore email is for this line: https://github.com/nearinfinity/node-persist/blob/master/lib/connection.js#L115
When I try to select
user.comments
I get nothing, query is:As you can see it uses user_id instead of user_email. I get the results if I delete this line: https://github.com/nearinfinity/node-persist/blob/master/lib/model.js#L334
What reasons do you have to delete foreignKey when you make reversed association in parent model? If child.key references parent.id, then parent.id must reference child.key, not child.id.