2014-07-14 13 views
5

Hat Waterline Unterstützung für die Durchführung von SQL-Joins (abgesehen von query())? Ich habe die Zuordnungen in meinen Modellen eingerichtet, aber die tatsächlichen SQL-Abfragen, die generiert werden, sind Schleifenauswahlanweisungen. Gibt es zur Zeit nur Unterstützung für "n + 1 selects"? Fehle ich irgendwo eine Konfigurationsoption? HierSails Waterline SQL beitreten füllen

ist ein Beispiel für ein 1-> Viele bevölkern, die Select-Anweisungen wird Looping: (Segel/Wasserlinie/Segel-mysql v0.10.0-rc8)

/** 
* City.js 
*/ 
module.exports = { 
"adapter": "someMysqlServer", 
"tableName": "city", 
autoCreatedAt: false, 
autoUpdatedAt: false, 
schema: true, 
attributes: { 
    "id": { 
     "type": "integer", 
     "primaryKey": true, 
     "autoIncrement": true, 
     "columnName": "ID" 
    }, 
    "Name": { 
     "type": "string", 
     "maxLength": 35 
    }, 
    "District": { 
     "type": "string", 
     "maxLength": 20 
    }, 
    "Population": { 
     "type": "integer" 
    }, 
    "CountryCode": { 
     //a city has one country 
     "model": "Country" 
    } 
}}; 




/** 
* Country.js 
*/ 

module.exports = { 
    "adapter": "someMysqlServer", 
    "tableName": "country", 
    autoCreatedAt: false, 
    autoUpdatedAt: false, 
    schema: true, 
    attributes: { 
     "id": { 
      "type": "string", 
      "required": true, 
      columnName: "Code", 
      "primaryKey": true, 
      "maxLength": 3 
     }, 
     "Name": { 
      "type": "string", 
      "maxLength": 52 
     }, 
     Continent: "string", 
     "Region": { 
      "type": "string", 
      "maxLength": 26 
     }, 
     SurfaceArea: "float", 
     IndepYear: "integer", 
     Population: "integer", 
     LifeExpectancy: "float", 
     GNP: "float", 
     GNPOld: "float", 
     LocalName: "string", 
     GovernmentForm: "string", 
     HeadOfState: "string", 
     Capital: "integer", 
     Code2: "string", 
     Cities: { 
      //country has many cities 
      "collection": "City", 
      "via": "CountryCode" 
     } 
    } 
}; 

Dann, wenn ich zu http://localhost:1337/country?populate=[Cities] navigieren meines Protokolle die folgende sQL ausgeführt wurde:

SELECT `country`.`Name`, `country`.`Continent`, `country`.`Region`, `country`.`SurfaceArea`, `country`.`IndepYear`, `country`.`Population`, `country`.`LifeExpectancy`, `country`.`GNP`, `country`.`GNPOld`, `country`.`LocalName`, `country`.`GovernmentForm`, `country`.`HeadOfState`, `country`.`Capital`, `country`.`Code2`,`country`.`Code` FROM `country` LIMIT 30 OFFSET 0 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ABW' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='AFG' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='AGO' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='AIA' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ALB' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='AND' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ANT' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ARE' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ARG' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ARM' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ASM' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ATA' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ATF' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='ATG' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='AUS' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='AUT' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='AZE' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BDI' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BEL' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BEN' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BFA' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BGD' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BGR' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BHR' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BHS' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BIH' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BLR' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BLZ' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BMU' LIMIT 30 
SELECT `city`.`Name`, `city`.`CountryCode`, `city`.`District`, `city`.`Population`, `city`.`ID` FROM `city` WHERE `city`.`CountryCode`='BOL' LIMIT 30 
+0

Ich bekomme das gleiche Problem. Testen, wenn ich die? Populate = [model] url verwende, bekomme ich das gleiche Verhalten. Wenn ich Model.find(). Populate ('OtherModel') explizit anrufe, bekomme ich eine etwas optimierte Version (mit IN), aber immer noch keine Join. – Rowan

Antwort

1

die .populate() Implementierung in der beta bewusst vereinfacht wird eine möglichst breite Reihe von Adaptern zu unterstützen. In der finalen v0.10-Version werden die unterstützten "core" SQL-Adapter (sails-mysql und sails-postgresql) tatsächliche Joins durchführen.

+0

@ sgress454Any Update zu diesem Thema? Ich habe gerade die sails 0.10.2 und sails-mysql 0.10.4 und mysql logs mit folgender Ausgabe installiert: '(SELECT * FROM Stadt AS Stadt WHERE CountryCode =" ABW "ORDER BY Stadt.ID ASC LIMIT 30) UNION (SELECT * FROM Stadt AS Stadt WHERE CountryCode = "AFG" ORDER BY city.id ASC LIMIT 30) UNION (AUSWAHL * AUS Stadt AS Stadt WHERE LandCode = "AGO" ORDER BY city.id ASC LIMIT 30) (...) ' – jaredfromsubway

+0

Dies ist weil Sie die Blueprint-Route mit einem Standardlimit von 30 Datensätzen erreichen. Sie können in diesem Fall keinen einfachen Join ausführen, da die Abfrage dann nur 30 Zeilen zurückgibt, wobei das, was Sie wirklich wollen, bis zu 900 Zeilen (30 Länder mit jeweils 30 Städten) umfasst. Daher die "UNION", die immer noch viel besser ist als eine separate Abfrage für jedes Land. Wenn Sie dies auf eine einzige Frage reduzieren können, wären wir sehr daran interessiert, es zu sehen! Siehe [diesen Artikel] (http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firleastmax-row-per-group-in-sql/) für weitere Informationen über die Problem. – sgress454