简体   繁体   English

SailsJS模型和数据库中的外键

[英]Foreign Keys in SailsJS Models and Databases

I've been trying to get a better understanding of how to setup Foreign Keys in SailsJS. 我一直在尝试更好地了解如何在SailsJS中设置外键。 I am currently working on a project for class in which my group needs to create an evaluation system with instructor and students profile to view the results. 我目前正在上一个班级项目,在这个项目中,我的小组需要创建一个包含教师和学生资料的评估系统,以查看结果。 I've seen some examples online, but I have seen varies formats and I'm not sure what the correct format is suppose to look like. 我在网上看到了一些示例,但看到的格式不尽相同,我不确定应该采用什么样的正确格式。

User Model 用户模型

/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {
  attributes: {
    // The user's anonymous ID (e.g. 1)
    anonymousID: {
        type: 'integer',
        autoIncrement: true
    },

    // The user's first name (e.g. Bob)
    firstName: {
      type: 'string',
      required: true
    },

    //The user's last name (e.g. Smith)
    lastName: {
      type: 'string',
      required: true,
    },

    // The user's full name (e.g. Bob Smith)
    fullName: {
      type: 'string',
      required: true
    },

    // The user's assigned NetID (e.g. abc123)
    netID: {
        type: 'string',
        primaryKey: true,
        required: true,
        unique: true
    },

    // The user's nine digit SchoolID (e.g. 000-000-000)
    schoolID: {
        type: 'integer',
        size: 9,
        required: true,
        unique: true
    },

    // The user's email address (e.g. netID@university.edu)
    email: {
        type: 'string',
        email: true,
        required: true,
        unique: true
    },

    // The encrypted password for the user (e.g. asdgh8a249321e9dhgaslcbqn2913051#T(@GHASDGA)
    encryptedPassword: {
      type: 'string',
      required: true
    },

    // The timestamp when the the user last logged in
    // (i.e. sent a username and password to the server)
    lastLoggedIn: {
      type: 'date',
      required: true,
      defaultsTo: new Date(0)
    },

    // The user's academic title (e.g. student)
    title: {
        state:{
            type : 'string',
            required: true,
            enum: ['Student', 'Faculty', 'Staff', 'Dean'],
        defaultsTo: 'Staff'
        }
    },

    // The user's academic classification (e.g. freshman)
    classification: {
        state: {
            type: 'string',
            required: true,
            enum: ['Freshman', 'Sophomore', 'Junior', 'Senior', 'Graduate', 'N/A']
        defaultsTo: 'N/A'
        }
    },

  }
};

Schedule Model 时间表模型

/**
* Schedule.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {
    // The CRN ID (e.g. 32458)
    courseID: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
    },

    // The Course Reference Name (e.g. MBW 1001)
    course: {
        type: 'string',
        size: 8,
        required: true
        // Add FK code from Course Table
    },

    // The Course Name (e.g. Magical Basket Weaving)
    title: {
        type: 'string',
        required: true
        // Add FK code from Course Table
    },

    // The Course Instructor (e.g. ab123)
    intructorID: {
        type: 'string',
        required: true
        // Add FK code from User Table
    },

    // The Term refers to the semester (e.g. Fall 2015)
    term: {
        type: 'string',
        required: true
    },
  }
};

Courses Model 课程模式

/**
* Course.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {
    // The Evaluation ID (e.g. 1)
    courseNum: {
        type: 'integer',
        autoIncrement: true
    },

    // The Department Name (e.g. Arts and Sciences)
    department: {
        type: 'string',
        required: true
    },

    // The Course Reference Name (e.g. MBW 1001)
    course: {
        type: 'string',
        primaryKey: true,
        size: 8,
        unique: true
    },

    // The Course Name (e.g. Magical Basket Weaving)
    title: {
        type: 'string',
        required: true
    },

  }
};

Enrolled Model 注册模型

/**
* Enrolled.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {
    // The Transaction ID (e.g. 32458)
    transactionID: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
    },

    // The CRN ID (e.g. 32458)
    courseID: {
        type: 'integer',
        required: true
      // Add FK code from Schedule Table
    },

    // The Course Instructor (e.g. ab123)
    instructorID: {
        type: 'string',
        required: true
        // Add FK code from Schedule Table
    },

    // The Course Instructor (e.g. ab123)
    studentID: {
        type: 'string',
        required: true
        // Add FK code from User Table
    },

    // The Term refers to the semester (e.g. Fall 2015)
    term: {
        type: 'string',
        required: true
    },

    // The Right to Submit an Evaluation (e.g. True or False)
    evaluationStatus: {
        type: 'boolean',
    },
  }
};

Evaluation Model 评估模型

/**
* Evaluation.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {
    // The Evaluation ID (e.g. 1)
    evaluationID: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
    },

    // The user's anonymous ID (e.g. 1)
    anonymousID: {
        type: 'string',
        required: true,
        // Add FK code from user table
    },

    // The Course Instructor (e.g. ab123)
    intructorID: {
        type: 'string',
        required: true
        // Add FK code from User Table
    },

    // The course's assigned CRN (e.g. 12343)
    courseID: {
        type: 'integer',
        required: true,
        size: 5
        // Add FK code from schedule table
    },

    // The Course Reference Name (e.g. MBW 1001)
    course: {
        type: 'string',
        size: 8,
    },

    // The rating of question 1
    ratingOne: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 2
    ratingTwo: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 3
    ratingThree: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 4
    ratingFour: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 5
    ratingFive: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 6
    ratingSix: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 7
    ratingSeven: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 8
    ratingEight: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 9
    ratingNine: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The rating of question 10
    ratingTen: {
        type: 'integer',
        required: true,
        size: 2
    },

    // The positive feedback from student
    positiveFeedback: {
        type: 'string',
        defaultsTo: 'N/A',
        size: 4000
    },

    // The negative feedback from student
    negativeFeedback: {
        type: 'string',
        defaultsTo: 'N/A',
        size: 4000
    },

    // The General Rating of Evaluation (e.g. 8.76, SUM(ratings)/TotalRatings)
    genRateEval: {
        type: 'float',
        required: true,
        size: 4
    },

    // The Inaproprate Flag (e.g. True or False)
    inaproprateFlag: {
        type: 'boolean',
    },
  }
};

I've included all five models I'm working with so everyone can get a complete picture of how everything will/should connect. 我已经包括了我正在使用的所有五个模型,因此每个人都可以全面了解所有事物/应该如何连接。

From my understanding, the foreign key should be setup like the code snippet below. 据我了解,外键应该像下面的代码片段一样进行设置。

Schedule Model (with Foreign Keys) 时间表模型(带有外键)

/**
* Schedule.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {
    // The CRN ID (e.g. 32458)
    courseID: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
    },

    // The Course Reference Name (e.g. MBW 1001)
    course: {
        // Add FK code from Course Table
      model: 'Course',
      via: 'course'
    },

    // The Course Name (e.g. Magical Basket Weaving)
    title: {
        // Add FK code from Course Table
      model: 'Course',
      via: 'title'
    },

    // The Course Instructor (e.g. ab123)
    intructorID: {
        // Add FK code from User Table
      model: 'User',
      via: 'netID'
    },

    // The Term refers to the semester (e.g. Fall 2015)
    term: {
        type: 'string',
        required: true
    },
  }
};

I'm not completely sure though if that's the proper way to setup foreign keys. 我不完全确定这是否是设置外键的正确方法。

Yes, its the right way of setting up a foreign key in sails js. 是的,这是在Sails js中设置外键的正确方法。 That being said, it varies on the type of association ie, whether the relation is one to one or one to many. 话虽如此,它取决于关联的类型,即关系是一对一还是一对多。

Taking the examples from sailsjs website, 以sailsjs网站为例,

One to One relation: 一对一关系:

myApp/api/models/pet.js

module.exports = {

    attributes: {
        name:'STRING',
        color:'STRING',
        owner:{
            model:'user'
        }
    }

}

myApp/api/models/user.js

module.exports = {

    attributes: {
        name:'STRING',
        age:'INTEGER',
        pony:{
            model: 'pet'
        }
    }

}

One to Many relation: myApp/api/models/pet.js 一对多关系: myApp/api/models/pet.js

module.exports = {

    attributes: {
        name:'STRING',
        color:'STRING',
        owner:{
            model:'user'
        }
    }

}

myApp/api/models/user.js

module.exports = {

    attributes: {
        name:'STRING',
        age:'INTEGER',
        pets:{
            collection: 'pet',
            via: 'owner'
        }
    }

}

Sailsjs Associations Sailsjs协会

您的主要问题是如何使用关联,因此请首先使用sails js官方网站http://sailsjs.org/documentation/concepts/models-and-orm/associations上的参考/文档,我想您所有的查询都将得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM