Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sequelize migration add column foreign key

queryInterface.addConstraint('table_name', {
  fields: ['column_name'],
  type: 'foreign key',
  name: 'custom_fkey_constraint_name', // optional
  references: {
    table: 'target_table_name',
    field: 'target_column_name'
  },
  onDelete: 'cascade',
  onUpdate: 'cascade'
});
Comment

define foreign key in sequelize migration

UserId: { // name of foreign key using naming convention
        type: Sequelize.DataTypes.INTEGER,
        references: {
          model: { tableName: 'Users' }, // provide table name
          key: 'id' // PK of the User Model
        },
        allowNull: false,
        onUpdate: 'cascade',
        onDelete: 'cascade',
},
Comment

sequelize migration add column

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.sequelize.transaction((t) => {
            return Promise.all([
                queryInterface.addColumn('table_name', 'field_one_name', {
                    type: Sequelize.STRING
                }, { transaction: t }),
                queryInterface.addColumn('table_name', 'field_two_name', {
                    type: Sequelize.STRING,
                }, { transaction: t })
            ])
        })
    },

    down: (queryInterface, Sequelize) => {
        return queryInterface.sequelize.transaction((t) => {
            return Promise.all([
                queryInterface.removeColumn('table_name', 'field_one_name', { transaction: t }),
                queryInterface.removeColumn('table_name', 'field_two_name', { transaction: t })
            ])
        })
    }
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: object.keys 
Javascript :: jquery unfocus 
Javascript :: js format time 
Javascript :: datatables add row 
Javascript :: how to check if a json object is empty 
Javascript :: icon refresh material ui 
Javascript :: node js to int 
Javascript :: pagination hook react 
Javascript :: jquery get radio button checked 
Javascript :: how to get enum item name in javascript 
Javascript :: javascript input checkbox name 
Javascript :: javascript alert random word 
Javascript :: javascript validate email 
Javascript :: remove an element from an array javascript 
Javascript :: javascript get typeof array 
Javascript :: Without using a new array or the reverse() method to Reverse an Array 
Javascript :: date and time in javascript 
Javascript :: javascript get html slider value 
Javascript :: javascript array flat 
Javascript :: contains whitespace js function 
Javascript :: append HTML elements in JavaScript 
Javascript :: first n even numbers sum javascript 
Javascript :: javascript string to float 
Javascript :: diff two arrays javascript 
Javascript :: get ascii value of char javascript 
Javascript :: react native image blur 
Javascript :: remove duplicates in json array based on two fields in lodash 
Javascript :: generate random date 
Javascript :: alert and prompt in javascript 
Javascript :: iterate over enum angular ngfor 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =