【问题标题】:How to set Primary keys in Sails js waterline database relationships如何在 Sails js 水线数据库关系中设置主键
【发布时间】:2020-03-22 20:10:35
【问题描述】:

我一直在研究官方文档中与sails JS水线数据库的关系。然而,我很难理解我应该如何设置我的外键,就像我在正常的 mysql 关系中所做的那样。 请注意,在提出这个问题之前,我已经阅读了https://sailsjs.com/documentation/concepts/models-and-orm/associations 此处的文档。

假设我有一个模型 PersonalInfo.js

module.exports = {

  attributes: {

    fullName:{
      type: 'string',
      required: true
    },

    phone:{
     type: 'string',
     required: true
   },

   location:{
     type: 'string',
     required: true
   },

   age:{
     type: 'integer',
     required: true
   },

   email:{
     type: 'string',
     required: true
   },

   gender:{
    type: 'string',
    required: true
  },

  userId:{
    type: 'integer',
    required: true,
  }
  
  },

};

我还有另一个模型 Archived.js,看起来像这样

module.exports = {

  attributes: {
    userId: {
      type: 'number',
      required: true,
      //unique: true,
    },
    comment:{
      type: 'string',
      required: true
    },
    createdBy:{
      type: 'number',
      required: true
    }
    
  },

};

已归档项目具有个人信息。完全知道这两个模型都包含 userId 属性,我想像这样使用相关的个人信息获取存档项目,我如何关联主键?

var archived = Archived.find().populate('personal');

【问题讨论】:

    标签: node.js sails.js waterline


    【解决方案1】:

    默认情况下,如果您不指定任何主键,sails 将生成主键 id

    如果您想要自定义数据作为主键,您可以覆盖模型中的id 属性并提供columnName

    id: {
      type: 'string',
      columnName: 'email_address',
      required: true
    }
    

    然后您可以使用以下命令查找记录:

    await User.find({ id: req.param('emailAddress' });
    

    Reference

    在您的情况下,似乎每个archived 都有一个personalInfo。所以这是来自archived 方面的one to one,但是,来自personalInfo 方面的one to many。为了对这些关系建模,在sails 中您可以执行以下操作:

    personalInfo.js

    module.exports = {
    
      attributes: {
    
        fullName:{
          type: 'string',
          required: true
        },
    
        phone:{
         type: 'string',
         required: true
       },
    
       location:{
         type: 'string',
         required: true
       },
    
       age:{
         type: 'integer',
         required: true
       },
    
       email:{
         type: 'string',
         required: true
       },
    
       gender:{
        type: 'string',
        required: true
      },
    
      userId:{
        type: 'integer',
        required: true,
      },
      archives: {
        collection: 'archived',
        via: 'info'
      }
    
      },
    
    };
    

    archived.js

    module.exports = {
    
      attributes: {
        userId: {
          type: 'number',
          required: true,
          //unique: true,
        },
        comment:{
          type: 'string',
          required: true
        },
        createdBy:{
          type: 'number',
          required: true
        },
    
        info: {
          model: 'personalinfo'  // sails works with small cases internally for models
        }
    
      },
    
    };
    

    完成此操作后,创建 archive 将是:

    await Archive.create({
      ...
    
      // Set the User's Primary Key to associate the info with the archive.
      info: 123
    });
    

    现在您终于可以在查询时填充info

    var archived = Archived.find().populate('info');

    【讨论】:

    • 感谢您与我们联系。我刚刚编辑了这个问题,我实际上是指数据库中关系的外键(我的错误)。我知道主键是自动生成的
    猜你喜欢
    • 2013-08-12
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多