在不使用主键的情况下在sails js中填充一对多关系

时间:2021-04-12 02:09:13

Every example code of one to many in sails/waterline documentation assumes the primary key is the association between two models (I think).

帆/水线文档中的一对多的示例代码假定主键是两个模型之间的关联(我认为)。

http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-to-many

However i have models that have a referral column that references some other values similar to

但是,我的模型有一个引用列,引用其他类似的值

User
{ 
    id (primary): <int>
    email: <string>
} 

recordings
{
    id (primary): <int>
    email: <that email from user>
}

atm im trying

atm我正在尝试

userModel.js
{
   attributes: {
      email: {
         type: 'string',
            size: 255,
            unique: true
         },
      },
      queries: { 
        columnName: 'email',
        model: 'recordings'
      }
      .....
   }
}

recordingsModel.js
{
   attributes: {
      email: {
         type: 'string',
            size: 255,
         },
      },
      queries: { 
        model: 'user'
      }
      .....
   }
}

and in the Controller

在控制器中

sails.models.user
     .find()
     .populate('queries')
     .exec(function (err, results) {

 });

But i get the error : ER_BAD_FIELD_ERROR: Unknown column '__queries.queries' in 'field list'

但我得到错误:ER_BAD_FIELD_ERROR:'字段列表'中的未知列'__queries.queries'

Does anyone have a good tutorial for one to many relationships in waterline because the documentation on there site is pretty bad so i feel im just not understanding how to design the models.

有没有人有一个关于水线中一对多关系的好教程,因为那里的文档非常糟糕所以我觉得我只是不了解如何设计模型。

1 个解决方案

#1


1  

From what I can gather, what you want is to be able to set up your userModel and recordingsModel models such that, by giving a recording a certain value for email, it will automatically be associated with any users that share that email. This is not something that the Waterline (the Sails ORM) supports.

从我可以收集的内容来看,您想要的是能够设置您的userModel和recordingsModel模型,这样,通过为电子邮件提供特定值的记录,它将自动与共享该电子邮件的任何用户相关联。这不是Waterline(Sails ORM)支持的东西。

Instead, your best option is to set the models up as indicated in the documentation:

相反,您最好的选择是按照文档中的说明设置模型:

// userModel.js
{
   attributes: {
      email: {
         type: 'string',
            size: 255,
            unique: true
         },
      },
      recordings: { 
        collection: 'recordingsModel',
        via: 'user'
      }
      .....
   }
}

// recordingsModel.js
{
   attributes: {
      email: {
         type: 'string',
            size: 255,
         },
      },
      user: { 
        model: 'userModel'
      }
   }
}

My guess is that you're trying to avoid having to look up a user ID in order to associate a new recording with it. You can do this if you make email the primary key of the userModel; then when you create a new recording, you can set its user to an email address and voila: the two are linked.

我的猜测是你试图避免查找用户ID以便将新录音与它相关联。如果您将电子邮件作为userModel的主键,则可以执行此操作;然后,当您创建新录制内容时,可以将其用户设置为电子邮件地址,然后将两者关联起来。

#1


1  

From what I can gather, what you want is to be able to set up your userModel and recordingsModel models such that, by giving a recording a certain value for email, it will automatically be associated with any users that share that email. This is not something that the Waterline (the Sails ORM) supports.

从我可以收集的内容来看,您想要的是能够设置您的userModel和recordingsModel模型,这样,通过为电子邮件提供特定值的记录,它将自动与共享该电子邮件的任何用户相关联。这不是Waterline(Sails ORM)支持的东西。

Instead, your best option is to set the models up as indicated in the documentation:

相反,您最好的选择是按照文档中的说明设置模型:

// userModel.js
{
   attributes: {
      email: {
         type: 'string',
            size: 255,
            unique: true
         },
      },
      recordings: { 
        collection: 'recordingsModel',
        via: 'user'
      }
      .....
   }
}

// recordingsModel.js
{
   attributes: {
      email: {
         type: 'string',
            size: 255,
         },
      },
      user: { 
        model: 'userModel'
      }
   }
}

My guess is that you're trying to avoid having to look up a user ID in order to associate a new recording with it. You can do this if you make email the primary key of the userModel; then when you create a new recording, you can set its user to an email address and voila: the two are linked.

我的猜测是你试图避免查找用户ID以便将新录音与它相关联。如果您将电子邮件作为userModel的主键,则可以执行此操作;然后,当您创建新录制内容时,可以将其用户设置为电子邮件地址,然后将两者关联起来。