SailsJS模型和数据库中的外键

时间:2015-12-06 17:38:12

标签: javascript foreign-keys sails.js models

我一直在努力更好地了解如何在SailsJS中设置外键。我目前正在开设一个课堂项目,我的小组需要创建一个评估系统,其中包含教师和学生的个人资料以查看结果。我在网上看过一些例子,但我看过不同的格式,我不确定假设的格式是什么样的。

用户模型

/**
* 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.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
    },
  }
};

课程模型

/**
* 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.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.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',
    },
  }
};

我已经包含了我正在使用的所有五个模型,因此每个人都可以全面了解一切将如何连接。

根据我的理解,外键应该像下面的代码片段一样设置。

计划模型(带外键)

/**
* 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
    },
  }
};

我不完全确定这是否是设置外键的正确方法。

2 个答案:

答案 0 :(得分:2)

是的,它是在风帆js中设置外键的正确方法。话虽如此,它的关联类型也不同,即关系是一对一还是一对多。

以sailsjs网站上的例子为例,

一对一关系:

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

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

function doFilter(data){
  return data.filter(function(d){return d.year == d3.select('#years').node().value && d.produce == d3.select('#inds').node().value});
}

  //onchange of years
    d3.select('#years')
            .on("change", function () {     
              var data1 = doFilter(json)
              updateGraph(data1);
            });
    //onchange of inds              
    d3.select('#inds')
            .on("change", function () {     
              var data1 = doFilter(json)
              updateGraph(data1);
    });

var data1 = doFilter(json)              
    // initial graph / defaults     
updateGraph(data1);

Sailsjs Associations

答案 1 :(得分:1)

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