express - Sequelize 关联不正确
问题描述
我想建立这样的关系: erd
我希望每个用户都包含消息列表,并且每条消息都包含有关发件人的信息。与我发布的 ERD 相关。
我用于迁移的代码:
module.exports = {
up: (queryInterface, Sequelize) =>
queryInterface.createTable('Messages', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
text: {
type: Sequelize.STRING,
allowNull: false,
},
toUserId: {
type: Sequelize.INTEGER,
// TODO changed from CASCADE - not sure about change
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'toUserId',
},
},
fromUserId: {
type: Sequelize.INTEGER,
// TODO changed from CASCADE - not sure about change
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'fromUserId',
},
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
}),
down: (queryInterface /* , Sequelize */) =>
queryInterface.dropTable('Messages'),
};
module.exports = {
up: (queryInterface, Sequelize) =>
queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
firstName: {
type: Sequelize.STRING,
allowNull: false,
},
lastName: {
type: Sequelize.STRING,
allowNull: false,
},
lat: {
allowNull: true,
type: Sequelize.INTEGER,
},
lng: {
allowNull: true,
type: Sequelize.INTEGER,
},
createdAt: {
allowNull: false,
type: Sequelize.DATEONLY,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATEONLY,
},
}),
down: (queryInterface /* , Sequelize */) => queryInterface.dropTable('Users'),
};
对于模型:
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
},
lat: {
allowNull: true,
type: DataTypes.INTEGER,
},
lng: {
allowNull: true,
type: DataTypes.INTEGER,
},
createdAt: {
allowNull: false,
type: DataTypes.DATEONLY,
},
updatedAt: {
allowNull: false,
type: DataTypes.DATEONLY,
},
});
User.associate = (models) => {
User.hasMany(models.Message, {
foreignKey: 'toUserId',
as: 'messages',
});
};
return User;
};
module.exports = (sequelize, DataTypes) => {
const Message = sequelize.define('Message', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
text: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
},
});
Message.associate = (models) => {
Message.belongsTo(models.User, {
foreignKey: 'toUserId',
});
};
return Message;
};
但是,当我尝试从 UserId 添加新消息时,显示为 Null 值。当我尝试获取消息时,它看起来像:
[
{
"id": 1,
"text": "mytext",
"createdAt": "2018-11-29T20:12:22.253Z",
"updatedAt": "2018-11-29T20:12:22.253Z",
"toUserId": 2,
"User": {
"id": 2,
"firstName": "name1",
"lastName": "name2",
"lat": null,
"lng": null,
"createdAt": "2018-11-29",
"updatedAt": "2018-11-29"
}
}
]
解决方案
根据您的说法,ERD
我认为您relationships
应该是这样的。
用户有很多消息
消息属于用户
在这种情况下,您的Message
迁移应该是这样的。
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
text: {
type: Sequelize.STRING,
allowNull: false,
},
fromUserId: {
type: Sequelize.INTEGER,
// TODO changed from CASCADE - not sure about change
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
},
},
请注意,我也删除了toUserId
列和此别名as: 'fromUserId',
。
在您的User
迁移中,您不需要更改任何内容是正确的。
在你的Message
模型中
Message.belongsTo(models.User, {
foreignKey: 'fromUserId',
});
};
请注意,您只有一个FK
,它是fromUserId
在你的User
模型中。
User.associate = (models) => {
User.hasMany(models.Message, {
foreignKey: 'fromUserId',
});
};
完成所有这些后,您将能够获取属于user_id
.
推荐阅读
- cuda - 带有推力执行策略的 __CUDA_ARCH__ 标志
- java - 使用不同的对象优化循环Java
- java - Branch.io (Android SDK) 和 GDPR
- mysql - 将删除的数据保留在其他表中
- php - 在刀片文件上使用 Poedit
- vba - 将多个文本文件导入单个工作表
- sas - 识别触发事件后的第一次发生
- android - 基于存储在 Firebase 生成的实例 ID 下的 lastMessageKey 的时间戳值排列 RecyclerView 项目的 Firebase 查询
- android - 图片不能在 iPhone 上使用,但它们可以在 Android 上使用?
- javascript - 当包裹在 "$(document).ready(function () {" 中时,Bootstrap selectpicker 会出错