2015-02-05 84 views
1

我开始使用Sails.js(我是一个绝对新手),我想创建以下类模型。
我有一个Student和一个SubscriptionList。学生拥有他/她订阅的subscriptionLists的集合,并且SubscriptionList知道所有订阅了它的学生。所以,这是一个双向的多对多关联。
我创建使用这些发电机模型:双向Sails.js上的多对多关联

sails generate model Student firstName:string lastName:string fileNumber:string career:string regid:string email:email 

sails generate model SubscriptionList name:string description:string 

因为我不知道如何来运行帆迁移(我没有在Rails的过去与此类似),在那之后我去了模型的文件,并添加此:

id: { 
    type: 'integer', 
    primaryKey: true, 
    autoIncrement: true 
}, 

subscriptionLists:{ 
    collection: "subscriptionLists", 
    via: "students" 
} 

Student模型,而这个:

id: { 
    type: 'integer', 
    primaryKey: true, 
    autoIncrement: true 
}, 

students:{ 
    collection: "students", 
    via: "subscriptionLists" 
} 

SubscriptionList型号,并从控制台运行sails lift。我得到了这个错误:

Error: Collection student has an attribute named subscriptionLists that is pointing to a collection named subscriptionlists which doesn't exist. You must first create the subscriptionlists collection. 

这是有道理的,因为我试图创建关系的两端(为我所了解)。
那么,如何在Sails.js中创建一个双向多对多关系,而不需要通过一个中间实体(我的意思是只使用常规交叉表)呢?

任何帮助将不胜感激。
此致敬礼。

回答

1

在sails/waterline中创建多对多关系时,将为您构建连接表。你的问题是一个错字。您指定的集合需要匹配它所指向的模型的模型名称。你已经复数了你的。需要是这样的:

学生:

id: { 
    type: 'integer', 
    primaryKey: true, 
    autoIncrement: true 
}, 

subscriptionLists:{ 
    collection: "subscriptionList", // match model name here 
    via: "students", // match attribute name on other model 
    dominant: true // dominant side 
} 

SubscriptionList:

id: { 
    type: 'integer', 
    primaryKey: true, 
    autoIncrement: true 
}, 

students:{ 
    collection: "student", // match model name 
    via: "subscriptionLists" // match attribute name 
} 

See the docs

+0

谢谢!这确实是一个愚蠢的错误。 – jmm 2015-02-06 13:13:15

+1

如果你习惯于Rails的方式进行复用,你可以通过在config/blueprints.js中设置'pluralize:true'来让Sails做类似的事情。当我开始使用Sails时,我用了一小会儿,但我停下了脚步,因为我发现自己变得很困惑。 – Jason 2015-02-11 01:36:03

+0

纠正我,如果我错了,但我不认为你应该需要'''显性:true''',除非你使用两个数据库。至少这就是它在这里所说的:http://sailsjs.org/#!/documentation/concepts/ORM/Associations/Dominance.html?q=what-about-noncrossadapter-associations – onetwopunch 2015-04-16 01:20:05