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

hasOne query based on referenced table #190

Closed
Locke opened this issue Jun 8, 2013 · 6 comments
Closed

hasOne query based on referenced table #190

Locke opened this issue Jun 8, 2013 · 6 comments

Comments

@Locke
Copy link

Locke commented Jun 8, 2013

I have a hasOne relationship and want to query based on the referenced table.

var Person = db.define('person', {
    name : String
});
var Animal = db.define('animal', {
    name : String
});
Animal.hasOne("owner", Person); // creates column 'owner_id' in 'animal' table

I want to do do something like:

SELECT * FROM animal JOIN person ON animal.owner_id = person.id WHERE person.name = "John";

Any ideas how I can achieve this? In the drivers there is opts.merge that seems to be exactly what I am searching for, but I can not find any documentation of it.

I already tried to pass find the options "__merge" and "extra", but ended up in a rather complex statement and failed to pass the condition.

It would be great if I could just do:

Animal.find(owner: Person.find("name": "John"))
@dresende
Copy link
Owner

I think there was a question before about this, I'm not sure, I can't remember how we solved it. This is interesting, I'm going to analyse it.

@dresende
Copy link
Owner

What do you think about this:

// .findByOwner() assuming you defined .hasOne("owner", ...)
Animal.findByOwner({ name: "John" }, function (err, animals) {
    // ...
});

@dresende
Copy link
Owner

I just added a commit with this new functionality. Please try it.

@Locke
Copy link
Author

Locke commented Jun 11, 2013

Thank you, that what I was looking for.

I now want to apply conditions to the animals.

My first try was chaining it, but then in findByX the cb stays null, wich results in Model.js that the argument[2] is an object (Model.js:237) and so the conditions are reset to null (Model.js:249) which then results in TypeError: Cannot call method 'hasOwnProperty' of null in line 251.

I got arround that problem adding if(arguments[i] === null) continue; and could do:

Animal.findByOwner({ name: "John" }).find({name: "myCatName"}).run(function (err, animals) {
    // ...
});

That works fine for me, but I've to mention that this could result into database errors if you have models with identical columnnames.

@dresende
Copy link
Owner

The problem with models with same property names is solvable, I'm going to pull an update that should fix this.

@Locke
Copy link
Author

Locke commented Jun 12, 2013

Thanks, that works fine.

@Locke Locke closed this as completed Jun 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants