【问题标题】:Sequelize Migration addIndex not adding index in descending orderSequelize Migration addIndex不按降序添加索引
【发布时间】:2020-01-23 09:05:42
【问题描述】:

我正在尝试在日期列的现有表 (Postgres) 上创建索引,以便我可以首先获取最新帖子

模型文件 feeditem.js

module.exports = (sequelize, DataTypes) => {
  const FeedItem = sequelize.define('FeedItem', {
    feedItemId: {
      //...
    },
    pubdate: {
      allowNull: false,
      type: DataTypes.DATE,
      validate: {
        isDate: true,
        notEmpty: true,
      },
    },
    link: {
      //...
    },
    title: {
      //...
    },
    description: {
      //...
    },
    summary: {
      //...
    },
    author: {
      //...
    },
    hash: {
      //...
    },
  }, {
    timestamps: false,
    underscored: true,
    indexes: [
      {
        fields: [{ attribute: 'pubdate', order: 'DESC' }],
        unique: false,
      },
    ],
  });

  FeedItem.associate = (models) => {
    // associations can be defined here
    //...
  };
  return FeedItem;
};

迁移文件 create-feed-item.js

module.exports = {
  up: (queryInterface, Sequelize) => queryInterface.createTable('feed_items', {
    feed_item_id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: Sequelize.INTEGER,
    },
    pubdate: {
      allowNull: false,
      type: Sequelize.DATE,
    },
    link: {
      allowNull: false,
      type: Sequelize.STRING,
    },
    title: {
      allowNull: false,
      type: Sequelize.STRING,
    },
    description: {
      type: Sequelize.TEXT,
    },
    summary: {
      type: Sequelize.TEXT,
    },
    author: {
      type: Sequelize.STRING,
    },
    hash: {
      allowNull: false,
      type: Sequelize.UUID,
      unique: true,
    },
  }),
  // eslint-disable-next-line no-unused-vars
  down: (queryInterface, Sequelize) => queryInterface.dropTable('feed_items'),
};

迁移文件 add-index.js

module.exports = {
  /*
    Add altering commands here.
    Return a promise to correctly handle asynchronicity.
    Example:
    return queryInterface.createTable('users', { id: Sequelize.INTEGER });
  */
  // eslint-disable-next-line no-unused-vars
  up: (queryInterface, Sequelize) => queryInterface.addIndex('feed_items', ['pubdate'], {
    fields: [{
      attribute: 'pubdate', order: 'DESC',
    }],
    unique: false,
    name: 'feed_items_pubdate_index',
  }),
  /*
    Add reverting commands here.
    Return a promise to correctly handle asynchronicity.
    Example:
    return queryInterface.dropTable('users');
  */
  // eslint-disable-next-line no-unused-vars
  down: (queryInterface, Sequelize) => queryInterface.removeIndex('feed_items', 'feed_items_pubdate_index'),
};

迁移以完美的顺序运行,首先创建表,然后添加索引。 日志记录已启用,当我检查日志时

预期: 它应该在 pubdate 上创建一个 DESC 索引,类似于

CREATE INDEX "feed_items_pubdate_index" ON "feed_items" ("pubdate" DESC)

实际输出

Executing (default): CREATE INDEX "feed_items_pubdate_index" ON "feed_items" ("pubdate")

你知道这里出了什么问题吗?

【问题讨论】:

    标签: postgresql sequelize.js database-migration


    【解决方案1】:

    使用唯一键:

    'use strict';
    module.exports = {
      up: (queryInterface, Sequelize) => {
        return queryInterface.createTable('Users', {
          id: {
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            type: Sequelize.INTEGER
          },
          name: {
            allowNull: true,
            type: Sequelize.STRING
          },
          order: {
            allowNull: false,
            type: Sequelize.INTEGER
          },
          createdAt: {
            allowNull: false,
            type: Sequelize.DATE
          },
          updatedAt: {
            allowNull: false,
            type: Sequelize.DATE
          }
        },
        {
          uniqueKeys: {
            actions_unique: {
              fields: ["name", "order"],
            },
          },
        }
        );
      },
      down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('Users');
      }
    };
    

    【讨论】:

      【解决方案2】:

      您的问题仍然相关,因为它没有记录在案。

      按降序定义索引的正确方法是在fields参数中扩展定义。

      所以迁移文件add-index.js 应该是这样的:

      module.exports = {
        ...
        up: (queryInterface, Sequelize) => queryInterface.addIndex(
          'feed_items', 
          [{
            attribute: 'pubdate', order: 'DESC',
          }], 
          {
            unique: false,
            name: 'feed_items_pubdate_index',
          }
        ),
        ...
      };
      

      已解决issue reference

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        • 2010-10-27
        • 1970-01-01
        • 2021-05-29
        • 1970-01-01
        • 2018-01-27
        相关资源
        最近更新 更多