在单次迁移中创建表并在其某些列上添加索引的正确方法是什么?
示例迁移:2012341234-create-todo.js
如何在“author_id”和“title”列上创建索引?
'use strict'; module.exports = { up: (queryInterface,Sequelize) => { return queryInterface.createTable('Todos',{ id: { allowNull: false,autoIncrement: true,primaryKey: true,type: Sequelize.INTEGER },author_id: { type: Sequelize.INTEGER,onDelete: 'CASCADE',references: { model: 'Authors',key: 'id',as: 'authorId' } },title: { type: Sequelize.STRING },content: { type: Sequelize.TEXT },createdAt: { allowNull: false,type: Sequelize.DATE },updatedAt: { allowNull: false,type: Sequelize.DATE } }); },down: (queryInterface,Sequelize) => { return queryInterface.dropTable('Todos'); } };
Sequelize文档表明将添加如下索引:
queryInterface.addIndex('Todos',['author_id','title']);