mysql - 属于或有一个 Sequelize

标签 mysql node.js sequelize.js

我有 2 个表:Users 和 Profile_Education。用户数据来自 auth0/login 表单,Profile_Education 来自使用 node.js/express 的 API。我希望 Profile_education 有一个外键来跟踪数据并根据登录的用户将其显示在配置文件中。

在我的项目中,我应该使用belongsTo还是hasOne,还是应该同时使用两者?

最佳答案

您可以同时使用它们,但在合适的地方使用它,按照 DOC :

When information about association is present in source model we can use belongsTo. In this case Player is suitable for belongsTo because it has teamId column.

Player.belongsTo(Team)  // `teamId` will be added on Player / Source model

When information about association is present in target model we can use hasOne. In this case Coach is suitable for hasOne because Team model store information about its Coach as coachId field.

Coach.hasOne(Team)  // `coachId` will be added on Team / Target model

我想这会消除你所有的疑虑,

关于mysql - 属于或有一个 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54636604/

相关文章:

javascript - 如何在我当前的 NodeJS 编码中使用 Promise Chaining

mysql - Sequelize MariaDB - 插入表情符号字符

android - 尝试访问从 Assets 到数据\数据\的复制数据库中的 DatabaseHelper 时出现空指针异常

node.js - SequelizeConnectionError : The server does not support SSL connections

Node.js Express 3

mysql - SQL 中的分组或 Sequelize 中的 where check 和 where in count 以及

javascript - Sequelize,使用 React 删除多行

java - 我如何获取单选按钮的值并使用 java eclipse 将其插入 mysql 数据库?

mysql - 对象引用未设置为对象的实例

php - 数据库设计 : implementing several types of the same entity