Sails.js 上的双向多对多关联
Bidirectional Many to many association on Sails.js
我开始使用 Sails.js(我绝对是新手),我想创建以下 class 模型。
我有一个 Student
和一个 SubscriptionList
。 Student 有一个 subscriptionLists
的集合,he/she 订阅了它,并且 SubscriptionList 知道所有订阅它的 Student。所以,这是一个双向的多对多关联。
我使用这些生成器创建了模型:
sails generate model Student firstName:string lastName:string fileNumber:string career:string regid:string email:email
sails generate model SubscriptionList name:string description:string
因为我不知道如何在 Sails 中 运行 迁移(我以前在 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?
中创建双向多对多关系,而不通过中间实体(我的意思是,仅使用常规交叉 table)
非常感谢任何帮助。
最好的问候。
当您在 sails/waterline 中创建多对多关系时,会为您构建一个连接 table。你的问题是一个错字。您指定的集合需要与其指向的模型的模型名称相匹配。你已经多元化了你的。需要这样:
学生:
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
}
订阅列表:
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true
},
students:{
collection: "student", // match model name
via: "subscriptionLists" // match attribute name
}
我开始使用 Sails.js(我绝对是新手),我想创建以下 class 模型。
我有一个 Student
和一个 SubscriptionList
。 Student 有一个 subscriptionLists
的集合,he/she 订阅了它,并且 SubscriptionList 知道所有订阅它的 Student。所以,这是一个双向的多对多关联。
我使用这些生成器创建了模型:
sails generate model Student firstName:string lastName:string fileNumber:string career:string regid:string email:email
sails generate model SubscriptionList name:string description:string
因为我不知道如何在 Sails 中 运行 迁移(我以前在 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?
非常感谢任何帮助。
最好的问候。
当您在 sails/waterline 中创建多对多关系时,会为您构建一个连接 table。你的问题是一个错字。您指定的集合需要与其指向的模型的模型名称相匹配。你已经多元化了你的。需要这样:
学生:
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
}
订阅列表:
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true
},
students:{
collection: "student", // match model name
via: "subscriptionLists" // match attribute name
}