January 2019
Intermediate to advanced
460 pages
10h 33m
English
We have to write a third migration, adding the userId column to our Post table, but also including it in our database Post model.
Generating a boilerplate migration file is very easy with the Sequelize CLI:
sequelize migration:create --migrations-path src/server/migrations --name add-userId-to-post
You can directly replace the content, as follows:
'use strict';module.exports = { up: (queryInterface, Sequelize) => { return Promise.all([ queryInterface.addColumn('Posts', 'userId', { type: Sequelize.INTEGER, }), queryInterface.addConstraint('Posts', ['userId'], { type: 'foreign key', name: 'fk_user_id', references: { table: 'Users', field: 'id', }, onDelete: 'cascade', onUpdate: 'cascade', }), ]); ...Read now
Unlock full access