简体   繁体   English

如何使用mysql在sails中定义外键关系

[英]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. 我对帆很了解,所有与ORM有关的文档都是尽可能基本的,所以我很难理解如何在我的模型中建立关系。 Here are my tables and models: http://pastebin.com/yt8jFTkk 以下是我的表格和型号: 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(): 我会满足于此并手动检查预期的应用程序类型和服务提供程序,因为令牌重叠似乎很不可能,并使用find()而不是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. 我想我对于sails / waterline如何知道我的AccountApplication模型上的account_id是帐户属性的外键有点遗憾。 What am I missing here? 我在这里错过了什么? Currently populate() on either account or application are doing nothing using the models above. 目前,使用上述模型,任何帐户或应用程序上的populate()都无效。

The problem is that you have duplicate account and accountId properties that mean the same thing. 问题是您有重复的accountaccountId属性,这意味着相同的事情。 What you want is account: { model: 'account', columnName: 'account_id', type: 'integer', required: true} and get rid of the accountId attribute. 您想要的是account: { model: 'account', columnName: 'account_id', type: 'integer', required: true}并删除accountId属性。

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 . 显然有一个错误,指定关联上的columnName会破坏关联,但似乎已经解决了https://github.com/balderdashy/waterline/issues/362

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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