Sequelize model links versus associations

Just starting to use Sequelize, and I set up a bunch of models and seeds, but I can’t understand the links and associations. I do not see an example of use for links, even if they do what I think they do, but I could not find a good explanation in the docs.

Is it redundant, having links 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; }; 
+13
source share
2 answers

Using references you create a link to another table without adding any restrictions or links. This means that this is simply a way to tell the database that this table has a link to another.

Creating an association will add a foreign key constraint to the attributes. And you can do all the magic behind this association function. those. User.getTasks();

More information about references here: https://sequelize.readthedocs.io/en/latest/docs/associations/#foreign-keys_1

About association here: http://docs.sequelizejs.com/manual/tutorial/associations.html

+10
source

https://github.com/sequelize/sequelize/issues/11026

 references: { model: { schema: 'schema_name', tableName: 'table_name' }, key: 'id' } 
0
source

Source: https://habr.com/ru/post/1271947/


All Articles