2016-06-07 8 views
0

두 개의 모델 Publication 및 Worksheet로 Sails 애플리케이션을 만들었습니다. 그들은 일대일 관계를 맺고 있습니다. Sails-postgresql은 제가 사용하고있는 어댑터입니다. 난 물 라인 orm을 사용하여 데이터베이스에 쿼리를 실행하고 있습니다. 나는 워크 시트와 함께 발행물 데이터를로드하려고 할 때 sort()를 사용하여 워크 시트의 필드에 따라 레코드를 정렬하려고 할 때 오류가 발생합니다.돛에 채워진 레코드 정렬

내 모델은 다음과 같습니다

Publication.js

module.exports = { 
    attributes: { 
     id: { 
      type: 'integer' 
      unique: true 
     }, 
     worksheetId: { 
      type: 'integer', 
      model : 'worksheet' 
     }, 
     status: { 
      type: 'string', 
      defaultsTo: 'active', 
      in : ['active', 'disabled'], 
     } 
    } 
} 

Worksheet.js

module.exports = { 
    attributes: { 
     id: { 
     type: 'integer', 
     unique: true 
     }, 
     name: 'string', 
     orderWeight: { 
     type: 'integer', 
     defaultsTo: 0 
     } 
    } 
} 

그래서 지금은 상태가 "활성"여기서 모든 출판물을로드하고 워크 시트를 채우려 데이터에.

Publication.find({ 
    where: { 
    status: 'active' 
    } 
}) 
.populate('worksheetId').limit(1) 
.exec(function (error, publications) { 
}) 

을 그리고 난 같은 데이터를 받고 있어요 :

은 그래서 쿼리를 실행하고있어 지금은 모든 작업 괜찮까지 그래서

{ 
    id : 1, 
    status : "active", 
    worksheetId : { 
     id : 1 
     name : "test", 
     orderWeight : 10 
    } 
} 

. 이제 한계를 10으로 늘리고 채워진 데이터에있는 "orderWeight"에 따라 데이터를 정렬하려고합니다. 처음에는 게시 ID와 쿼리에 따라 전체 데이터를 정렬했습니다.

Publication.find({ 
    where: { 
    status: 'active' 
    } 
}) 
.populate('worksheetId').sort('id ASC').limit(10) 
.exec(function (error, publications) { 
}) 

그래서 나는 "orderWeight"

Publication.find({ 
    where: { 
     status: 'active' 
    } 
}) 
.populate('worksheetId').sort('worksheetId.orderWeight ASC').limit(10) 
.exec(function (error, publications) { 
}) 

의 데이터를 정렬하는 유사한 쿼리를 해고 그리고이 쿼리는 worksheetId.orderWeight가 게시 테이블에 열이 아니라고 나에게 오류를주고있다. 그래서 게시 테이블에없는 채워진 데이터에 대해이 정렬 쿼리를 실행하려고합니다.

제 기대 결과를 얻을 수있는 방법을 알려주세요.

sort() 메서드 외에도 채워진 데이터에 대해 find 명령을 실행하여 워크 시트 이름이 특정 키와 일치하는 게시를 얻고 싶습니다.

답변

0

기본적으로 당신이하려는 것은 연관 속성입니다. This has been in the waterline roadmap since 2014, but it's still not supported이므로 해결 방법을 찾아야합니다.

하나의 옵션은 돛 이후 (작동하지 않는 .sort('worksheetId.orderWeight ASC') 즉) 당신은 원시 쿼리를 사용하지 않고 모델을 통해 조회하지 않습니다는 Worksheet 모델을 조회하고 Publication을 채울 것입니다. 죄송 합니다만 active 플래그를 Worksheet으로 이동해야 할 수 있습니다. 예를 들어 : 그들은 일대일있어 이후

Worksheet.find({ 
    status: 'active' 
}) 
.populate('publication') // you should also add publication to Worksheet.js 
.sort('orderWeight ASC') 
.limit(10) 

다른 방법으로는, 하나 개의 모델로 WorksheetPublication을 결합 할 수 있습니다. 아마 이상적은 아니지만 sails.js와 Waterline은 관계형 데이터로 작업하기가 매우 어렵습니다. 제가하고있는 프로젝트에서 쿼리의 절반은 돛에 대한 포스트그레스에 대한 열악한 지원 때문에 생 쿼리라고 추정 할 수 있습니다. 프레임 워크는 MongoDB, although it claims to "just work" with any of the "supported" DBs을 사용하는 것에 편향되어 있습니다.