Just starting to use Sequelize and I've setup a bunch of models and seeds, but I can't figure out references vs associations. I don't see the use case for references if they even do what I think they do, but I couldn't find a good explanation in the docs.
Is this redundant having references and associations?
module.exports = (sequelize, DataTypes) => {
const UserTask = sequelize.define('UserTask',
{
id: {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
},
userId: {
type: DataTypes.UUID,
references: { // <--- is this redundant to associate
model: 'User',
key: 'id'
}
}
// ... removed for brevity
},
{
classMethods: {
associate: models => { <--- makes references redundant?
UserTask.belongsTo(models.User, {
onDelete: 'CASCADE',
foreignKey: {
fieldName: 'userId',
allowNull: true,
require: true
},
targetKey: 'id'
});
}
}
}
);
return UserTask;
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…