Sequelize.js onDelete: ‘cascade’ is not deleting records sequelize

I believe you are supposed to put the onDelete in the Category model instead of in the products model. module.exports = function(sequelize, DataTypes) { var Category = sequelize.define(‘Category’, { name: { type: DataTypes.STRING, allowNull: false } }, { associate: function(models) { Category.hasMany(models.Product, { onDelete: ‘cascade’ }); } }); return Category }

Using group by and joins in sequelize

This issue has been fixed on Sequelize 3.0.1, the primary key of the included models must be excluded with attributes: [] and the aggregation must be done on the main model (infos in this github issue). Thus for my use case, the code is the following models.contracts.findAll({ attributes: [‘id’, [models.sequelize.fn(‘sum’, models.sequelize.col(‘payments.payment_amount’)), ‘total_cost’]], include: [ { … Read more

Sequelize Create Database

I may have a reasonable solution. I am sure it could be written more cleanly though. For this example I’m using Postgres, the answer would be slightly different for MySQL. I’m heavily borrowing from this answer: node-postgres create database In database.js I have the following init function var Sequelize = require(‘sequelize’), pg = require(‘pg’); module.exports.init … Read more

Join across multiple junction tables with Sequelize

Assuming the following relations: User.belongsToMany(Team, { through: ‘users_teams’}); Team.belongsToMany(User, { through: ‘users_teams’}); Folder.belongsToMany(Team, { through: ‘teams_folders’}); Team.belongsToMany(Folder, { through: ‘teams_folders’}); You should be able to load everything in one go using nested includes: User.findAll({ include: [ { model: Team, include: [ Folder ] } ] }); You seem to be on the right track already … Read more

Counting associated entries with Sequelize

Use findAll() with include() and sequelize.fn() for the COUNT: Location.findAll({ attributes: { include: [[Sequelize.fn(“COUNT”, Sequelize.col(“sensors.id”)), “sensorCount”]] }, include: [{ model: Sensor, attributes: [] }] }); Or, you may need to add a group as well: Location.findAll({ attributes: { include: [[Sequelize.fn(“COUNT”, Sequelize.col(“sensors.id”)), “sensorCount”]] }, include: [{ model: Sensor, attributes: [] }], group: [‘Location.id’] })