0

I created a Sails application with two models Publication and Worksheet. They are having a one-to-one relationship. Sails-postgresql is the adapter I'm using. I'm using waterline orm to fire query to the database. I'm When I am trying to load publications data along with worksheet and then sort the records depending on a field in the Worksheet using sort() I'm getting an error.

My model is:

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
      }
  }
}

So now I want to load all the publication where status is "active" and populate worksheet in the data.

So I'm executing the query:

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

And I'm getting a data like :

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

So till now it's all working fine. Now I want to increase the limit to 10 and want to sort the data depending on "orderWeight" which is in the populated data. Initially I sorted the whole data depending on publication id and the query worked.

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

So I fired similar query to sort the data on "orderWeight"

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

And this query is giving me error that worksheetId.orderWeight is not a column on the publication table. So I want to fire this sort query on the populated data not on the publication table.

Please let me know how I can get my expected result.

Apart from sort() method I also want to run some find command to the populated data to get those publication where the worksheet name matches with certain key as well.

Indranil Mondal
  • 2,799
  • 3
  • 25
  • 40

1 Answers1

0

Basically, what you're trying to do, is query an association's attribute. This has been in the waterline roadmap since 2014, but it's still not supported, so you'll have to figure out a workaround.

One option is to query the Worksheet model, and populate the Publication, since sails doesn't let you query across models without using raw queries (i.e. .sort('worksheetId.orderWeight ASC') doesn't work). Unfortunately, you might have to move the active flag to the Worksheet. For example:

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

Alternatively, you could combine Worksheet and Publication into one model, since they're one-to-one. Probably not ideal, but sails.js and Waterline make it very difficult to work with relational data - I'd estimate that half of the queries in the project I'm working on are raw queries due to sails' poor support of postgres. The framework is pretty biased towards using MongoDB, although it claims to "just work" with any of the "supported" DBs.

solumos
  • 11
  • 2