javascript - Sequelize.js 一对多关系外键

标签 javascript mysql node.js express sequelize.js

我正在使用 Node.js/Express 和 MySQL 以及 Sequelize.js ORM 创建一个调查应用程序。

我无法正确设置两个模型之间的关系。我想在答案表中包含问题的 qId 外键。

// define the Questions table
var Questions = sequelize.define('Questions', {
  qId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
  question: Sequelize.STRING
}, {
  timestamps: false
});

// define the Answers table
var Answers = sequelize.define('Answers', {
  aId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
  answer: Sequelize.STRING,
  answer_count: { type: Sequelize.INTEGER, defaultValue: 0}
}, {
  timestamps: false
});

// define one-to-many relationship
Questions.hasMany(Answers, {as: 'Answers', foreignKey: 'qId'});

Questions.sync({force: true}).then(function() {
  // OPTIONAL: create a new question upon instantiating the db using sequelize
  Questions.create({question: 'what is your language?'});
  Questions.create({question: 'what is your drink?'});
  console.log('created Questions table');
  }).catch(function(error) {
    console.log('error creating Questions table');
  });

Answers.sync({force: true}).then(function() {
  Answers.create({answer: 'python', qId: 1});
  Answers.create({answer: 'javascript', qId: 1});
  Answers.create({answer: 'ruby', qId: 1});
  Answers.create({answer: 'c++', qId: 1});
  Answers.create({answer: 'manhattan', qId: 2});
  Answers.create({answer: 'cosmopolitan', qId: 2});
  console.log('created Answers table');
}).catch(function(error) {
  console.log('error creating Answers table');
});

但是当我进行 MySQL 查询时:

select * from Questions, Answers where Answers.qId=2;

它显示以下内容:

mysql> select * from Answers;
+-----+--------------+--------------+------+
| aId | answer       | answer_count | qId  |
+-----+--------------+--------------+------+
|   1 | python       |            0 |    1 |
|   2 | javascript   |            0 |    1 |
|   3 | ruby         |            0 |    1 |
|   4 | c++          |            0 |    1 |
|   5 | manhattan    |            0 |    2 |
|   6 | cosmopolitan |            0 |    2 |
+-----+--------------+--------------+------+
6 rows in set (0.00 sec)

mysql> select * from Questions;
+-----+------------------------+
| qId | question               |
+-----+------------------------+
|   1 | what is your language? |
|   2 | what is your drink?    |
+-----+------------------------+
2 rows in set (0.00 sec)

mysql> select * from Questions, Answers where Answers.qId=2;
+-----+------------------------+-----+--------------+--------------+------+
| qId | question               | aId | answer       | answer_count | qId  |
+-----+------------------------+-----+--------------+--------------+------+
|   1 | what is your language? |   5 | manhattan    |            0 |    2 |
|   1 | what is your language? |   6 | cosmopolitan |            0 |    2 |
|   2 | what is your drink?    |   5 | manhattan    |            0 |    2 |
|   2 | what is your drink?    |   6 | cosmopolitan |            0 |    2 |
+-----+------------------------+-----+--------------+--------------+------+

什么时候显示

mysql> select * from Questions, Answers where Answers.qId=2;
+-----+------------------------+-----+--------------+--------------+------+
| qId | question               | aId | answer       | answer_count | qId  |
+-----+------------------------+-----+--------------+--------------+------+ 
|   2 | what is your drink?    |   5 | manhattan    |            0 |    2 |
|   2 | what is your drink?    |   6 | cosmopolitan |            0 |    2 |
+-----+------------------------+-----+--------------+--------------+------+

我已经查看文档几个小时了,非常感谢任何帮助 :) 谢谢。

最佳答案

你的sql查询应该是;

SELECT * FROM Questions, Answers WHERE Answers.qId = 2 GROUP BY Answers.aId;

SELECT * FROM Questions, Answers WHERE Answers.qId = Questions.qId and Questions.qId = 2;

这个查询会告诉你这个;

| qId |            question | aId |        answer | answer_count | qId |
|-----|---------------------|-----|---------------|--------------|-----|
|   2 | what is your drink? |   5 |     manhattan |            0 |   2 |
|   2 | what is your drink? |   6 | cosmopolitan  |            0 |   2 |

并且您应该附加此关联;

Answer.belongsTo(Question, {
  "constraints": true,
  "foreignKey": 'qId'
});

在你应该能够像这样使用这个关系/连接之后;

Question
  .findOne({
    "where": {
      "qId": 2
    },
    "include": [Answer]
  })
  .then(function(question) {
    // should show question with its answers
    console.log(question);

    // should show just answers of this question
    console.log(question.Answers);
  });

关于javascript - Sequelize.js 一对多关系外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32000243/

相关文章:

php - 如何在刷新后保持图像按钮状态

javascript - 未找到 Firebase Admin SDK 函数

javascript - signalR 未定义

javascript - 数组数组到字典 JavaScript

php - 当未明确给出新值时,让字段采用另一个字段的值

php - Android JSON解析返回null

javascript - 这是重定向的正确方法吗?

javascript - NodeJS, Async forEachSeries 执行顺序

javascript - 使用 Ajax 调用从 NodeJS 向客户端发送数据

javascript - Nodejs 运行每日任务