我想建立这样的关系: 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"
}
}
]
答案 0 :(得分:0)
根据您的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
的消息。