How to define foreign key relationships in sails with mysql

I'm really new to sailing and all the ORM related documentation is about as simple as you could get, so I'm struggling to figure out how to establish relationships in my models. Here are my tables and models: http://pastebin.com/yt8jFTkk

Now I am trying to do something like:

AccountApplication.findOne({
    token: 'someTokenHere', 
    application.type: 'foo', 
    application.serviceProvider: 'bar'
}).populate('application')
.populate('account').exec(...)...

      

I would settle for this and manually check the expected type of application and service provider, since overlapping tokens seems rather unlikely and uses find () instead of findOne ():

AccountApplication.findOne({
    token: 'someTokenHere'
}).populate('application')
.populate('account').exec(...)...

      

I think I am a bit lost on how the sails / waterlines know that the account_id in my AccountApplication model is a foreign key to the account property. What am I missing here? Currently populate () in any account or application does nothing using the models above.

+3


source to share


1 answer


The problem is that you have duplicate properties account

and accountId

that mean the same thing. You want account: { model: 'account', columnName: 'account_id', type: 'integer', required: true}

and get rid of the attribute accountId

.



The important thing is that you only have one attribute for the association that stores the underlying data; you do not need to specify an additional foreign key column. Apparently there was a bug where pointing columnName

to associations would break the association, but it seems to have been resolved https://github.com/balderdashy/waterline/issues/362 .

+2


source







All Articles