Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugs in custom primary key handling #63

Open
anongit opened this issue Apr 13, 2013 · 0 comments
Open

Bugs in custom primary key handling #63

anongit opened this issue Apr 13, 2013 · 0 comments

Comments

@anongit
Copy link

anongit commented Apr 13, 2013

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:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant