dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

findBySmth has doubles #846

Open naturalcod opened 4 years ago

naturalcod commented 4 years ago

Hi. i broke my head.

I have this: this.req.models.Tour.findByInfo({ Charter: this.req.query.Charter, }, { autoFetch: false }).where("Active = 1",(err, tours) => { this.res.end(JSON.stringify(tours)) })

This is what I get. At the output, I get:

[ { "Title": "Тур в горы", "Rating": 9, "Description": "Ахуенный тур", "Active": true, "MinDate": null, "MaxDate": null, "id": 1, "owner_id": 1, "town_id": 1 }, { "Title": "Тур в горы", "Rating": 9, "Description": "Ахуенный тур", "Active": true, "MinDate": null, "MaxDate": null, "id": 1, "owner_id": 1, "town_id": 1 }, { "Title": "Тур в горы", "Rating": 9, "Description": "Ахуенный тур", "Active": true, "MinDate": null, "MaxDate": null, "id": 1, "owner_id": 1, "town_id": 1 }, { "Title": "Тур в горы", "Rating": 9, "Description": "Ахуенный тур", "Active": true, "MinDate": null, "MaxDate": null, "id": 1, "owner_id": 1, "town_id": 1 } ]

I understand what the problem is, so JOIN works. SQL:

SELECT t1.Title, t1.Rating, t1.Description, t1.Active, t1.MinDate, t1.MaxDate, t1.id, t1.owner_id, t1.town_id FROM Tour t1 JOIN TourInfo t2 ON t2.tour_id = t1.id WHERE (t2.Charter = '1' AND t2.Price BETWEEN 0 AND 999999999) AND (Active = 1)

BUT i NEED so SQL:

SELECT DISTINCT(t1.id), t1.Title, t1.Rating, t1.Description, t1.Active, t1.MinDate, t1.MaxDate, t1.owner_id, t1.town_id FROM Tour t1 JOIN TourInfo t2 ON t2.tour_id = t1.id WHERE (t2.Charter = '1' AND t2.Price BETWEEN 0 AND 999999999) AND (Active = 1)

OR

SELECT t1.id, t1.Title, t1.Rating, t1.Description, t1.Active, t1.MinDate, t1.MaxDate, t1.owner_id, t1.town_id FROM Tour t1 JOIN TourInfo t2 ON t2.tour_id = t1.id WHERE (t2.Charter = '1' AND t2.Price BETWEEN 0 AND 999999999) AND (Active = 1) GROUP BY t1.id