node.jssails.jssails-postgresql

Sails.js: Could not populate model because of ambiguous usage


I'm having two models of let's say A and B. It's an 1:n relation, where one A owns one B, and B can be owned my multiple As.

Now, if possible, B shouldn't have to know about all the As it's referenced in, so I'm only associating on the A side.

Now I want to search for all As that own a B with a certain criteria. I've basically tried this:

A.find()
  .populate('B', {
    where: {
      someAttr: 1
    }
  })

Looking at the documentation I find this pretty similar to the example there.

Now I'm getting the following error:

Could not populate B because of ambiguous usage. This is a singular ("model") association, which means it never refers to more than one associated record. So passing in subcriteria (i.e. as the second argument to .populate()) is not supported for this association, since it generally wouldn't make any sense. But that's the trouble-- it looks like some sort of a subcriteria (or something) was provided!

Can I somehow make this query work without giving B a reference back on A? I feel like this is unnecessary bloating of the models.

I'm using Sails 1.0.1 right now.


Solution

  • Looking into the docs about the second argument subcriteria to populate :

    When populating collection associations between two models which reside in the same database, a Waterline criteria may be specified as a second argument to populate. This will be used for filtering, sorting, and limiting the array of associated records (e.g. snacks) associated with EACH primary record.

    One way you can do this is to retrieve all the records then filter them, or use a native query directly.