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

标签 mysql node.js sails.js waterline

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

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

但是我的模型有一个推荐列,引用了一些类似于

的其他值
User
{ 
    id (primary): <int>
    email: <string>
} 

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

我正在尝试取款机

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'
      }
      .....
   }
}

在 Controller 中

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

 });

但是我得到了错误 : ER_BAD_FIELD_ERROR:“字段列表”中的未知列“__queries.queries”

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

最佳答案

据我所知,您想要的是能够设置您的 userModelrecordingsModel 模型,以便通过为记录赋予特定值 email,它将自动与共享该电子邮件的任何用户相关联。这不是 Waterline(Sails ORM)支持的东西。

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

// 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'
      }
   }
}

我的猜测是您试图避免必须查找用户 ID 才能将新录音与其关联。如果将 email 作为 userModel 的主键,就可以这样做;然后当你创建一个新的记录时,你可以将它的 user 设置为一个电子邮件地址,瞧:这两个是链接的。

关于mysql - 在不使用主键的情况下在 sails js 中填充一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37035841/

相关文章:

php - 为什么在我的代码中 PDO lastInsertId() 返回 0?

node.js - Nodejs +Nodemailer @4.6.4 + 电子邮件模板 @3.6.0

node.js - 停止 apache 并运行 Node 服务器

sails.js - SailsJS 最佳实践,在其他模型初始化之前用数据播种数据库

java - 将查询字符串存储在数据库中通常被认为是可行的吗?

MySQL 错误: `query':Ruby 文件上的键 3 (Mysql2::Error) 重复条目 ''

postgresql - Heroku 上的 sailsjs 未在 postgresql 中创建模型

node.js - Sailsjs - Hook orm 加载时间过长 - 模数

MySQL优化连接查询 "OR"条件

node.js - Node.js 中的 PostgreSQL 多行更新