Sequelize – update record, and return result

Here’s what I think you’re looking for. db.connections.update({ user: data.username, chatroomID: data.chatroomID }, { where: { socketID: socket.id }, returning: true, plain: true }) .then(function (result) { console.log(result); // result = [x] or [x, y] // [x] if you’re not using Postgres // [x, y] if you are using Postgres }); From Sequelize docs: The … Read more

sequelize.js TIMESTAMP not DATETIME

Just pass in ‘TIMESTAMP’ string to your type module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.createTable(‘users’, { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, created_at: { type: ‘TIMESTAMP’, defaultValue: Sequelize.literal(‘CURRENT_TIMESTAMP’), allowNull: false }, updated_at: { type: ‘TIMESTAMP’, defaultValue: Sequelize.literal(‘CURRENT_TIMESTAMP’), allowNull: false } }); } };

Sequelize.js foreign key

Before I had the same problem, and solved when I understood the functioning of settings Sequelize. Straight to the point! Suppose we have two objects: Person and Father var Person = sequelize.define(‘Person’, { name: Sequelize.STRING }); var Father = sequelize.define(‘Father’, { age: Sequelize.STRING, //The magic start here personId: { type: Sequelize.INTEGER, references: ‘persons’, // <<< … Read more

How to get a distinct value of a row with sequelize?

You can specify distinct for one or more attributes using Sequelize.fn Project.findAll({ attributes: [ // specify an array where the first element is the SQL function and the second is the alias [Sequelize.fn(‘DISTINCT’, Sequelize.col(‘country’)) ,’country’], // specify any additional columns, e.g. country_code // ‘country_code’ ] }).then(function(country) { })

How to get a distinct count with sequelize?

UPDATE: New version There are now separate distinct and col options. The docs for distinct state: Apply COUNT(DISTINCT(col)) on primary key or on options.col. You want something along the lines of: MyModel.count({ include: …, where: …, distinct: true, col: ‘Product.id’ }) .then(function(count) { // count is an integer }); Original Post (As mentioned in the … Read more

sequelize.import is not a function

The error is caused by using the sequelize import object. Instead you should use Node’s built in CommonJS require function. So change this line in your models/index.js: const model = sequelize[‘import’](path.join(__dirname, file)) to: const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes) You can also just regenerate the models directory and readd your models without the old index.js … Read more