首页 > 解决方案 > Adonis:如何为已存在的列添加索引?

问题描述

我的表中有一些现有的列,我想在这些列上添加索引。我该如何进行?从文档中它说:

table..index([indexName], [indexType]) // 将列指定为索引。

但是 indexName 是什么意思呢?

标签: adonis.js

解决方案


我应该去看 Knex.js 文档

index — table.index(columns, [indexName], [indexType])

在给定列上向表中添加索引。除非指定 indexName,否则使用使用列的默认索引名称。可以选择为 PostgreSQL 和 MySQL 指定 indexType。Amazon Redshift 不允许创建索引。

这是我的做法:

/**************************************************************************
 * IMPORTS
 ***************************************************************************/

// Providers
const Schema = use('Schema')

/**************************************************************************
 * MIGRATIONS
 ***************************************************************************/

class AddDomainsIndexSchema extends Schema {
  up() {
    this.table('domains', (table) => {
      table.index('domain')
      table.index('fetched')
    })
  }

  down() {
    this.table('domains', (table) => {
      table.dropIndex('domain')
      table.dropIndex('fetched')
    })
  }
}

module.exports = AddDomainsIndexSchema

推荐阅读