简体   繁体   中英

How to define foreign key relationships in sails with mysql

I'm really new to sails and all of the documentation to do with ORM is about as basic as you could get, so I'm struggling to understand how to set up relationships in my models. Here are my tables and models: http://pastebin.com/yt8jFTkk

Now, what I'm trying to do is 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 for the expected application type and service provider as token overlap seems pretty improbable and use a find() instead of findOne():

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

I guess I'm at a bit of a loss as to how sails / waterline is to know that the the account_id on my AccountApplication model is the foreign key for the account property. What am I missing here? Currently populate() on either account or application are doing nothing using the models above.

The problem is that you have duplicate account and accountId properties that mean the same thing. What you want is account: { model: 'account', columnName: 'account_id', type: 'integer', required: true} and get rid of the accountId attribute.

The important thing is that you only have one attribute for the association that stores the underlying data; you don't need to specify the foreign key column additionally. Apparently there was a bug where specifying columnName on an association would break the association, but it seems to have been resolved https://github.com/balderdashy/waterline/issues/362 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM