Answers for "sequelize associations"

1

sequelize associations

Creating the standard relationships
To create a One-To-One relationship, the hasOne and belongsTo associations are used together;
To create a One-To-Many relationship, the hasMany and belongsTo associations are used together;
To create a Many-To-Many relationship, two belongsToMany calls are used together.
Posted by: Guest on August-18-2021
0

sequelize associations

const User = sequelize.define('user', { name: DataTypes.STRING }, { timestamps: false });
const Task = sequelize.define('task', { name: DataTypes.STRING }, { timestamps: false });
const Tool = sequelize.define('tool', {
  name: DataTypes.STRING,
  size: DataTypes.STRING
}, { timestamps: false });
User.hasMany(Task);
Task.belongsTo(User);
User.hasMany(Tool, { as: 'Instruments' });


//access the model
const tasks = await Task.findAll({ include: User });

//output

[{
  "name": "A Task",
  "id": 1,
  "userId": 1,
  "user": {
    "name": "John Doe",
    "id": 1
  }
}]
Posted by: Guest on December-30-2020

Browse Popular Code Answers by Language