【问题标题】:Knex and PostgreSQL: How to drop an unique index?Knex 和 PostgreSQL:如何删除唯一索引?
【发布时间】:2021-02-27 19:26:43
【问题描述】:

我有一个使用 knex 进行迁移的应用。早些时候有人使用knex.raw 在一列上创建了唯一索引,如下所示:

const createProgrammeTable = knex => knex.raw(`
  CREATE TABLE programme (
    id             serial PRIMARY KEY,
    date           date NOT NULL,
    published      date,
    description    text
  );
  CREATE UNIQUE INDEX programme_date_index ON programme(date);
`)

使用 psql 打印出数据库时,我得到以下信息:

server=# \d programme;
                              Table "public.programme"
  Column   |  Type   | Collation | Nullable |                Default
-----------+---------+-----------+----------+---------------------------------------
 id        | integer |           | not null | nextval('programme_id_seq'::regclass)
 date      | date    |           | not null |
 published | boolean |           | not null |
Indexes:
    "programme_pkey" PRIMARY KEY, btree (id)
    "programme_date_index" UNIQUE, btree (date)

现在我想删除唯一约束,但我不确定如何。我已经读到我可以这样做:

ALTER TABLE programme DROP INDEX programme_date_index;

但是使用knex.raw 这样做会出现此错误:

migration failed with error: ALTER TABLE programme DROP INDEX programme_date_index - syntax error at or near "programme_date_index"
error: ALTER TABLE programme DROP INDEX programme_date_index - syntax error at or near "programme_date_index"
    at Connection.parseE (/Users/my-path/node_modules/pg/lib/connection.js:555:11)
    at Connection.parseMessage (/Users/my-path/node_modules/pg/lib/connection.js:380:19)
    at Socket.<anonymous> (/Users/my-path/node_modules/pg/lib/connection.js:120:22)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
error Command failed with exit code 1.

我也尝试过像这样使用dropUnique

exports.up = function(knex, Promise) {
  return knex.schema.alterTable("programme", function(t) {
    t.dropUnique("date", "programme_date_index")
  })
}

这给出了以下错误:

migration failed with error: alter table "programme" drop constraint "programme_date_index" - constraint "programme_date_index" of relation "programme" does not exist

版本

Knex CLI 版本:0.20.15 / Knex 本地版本:0.20.15

server=# select version();
                                                     version
------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit

有人可以帮我/把我推向正确的方向吗?

【问题讨论】:

    标签: postgresql knex.js knexjs


    【解决方案1】:

    将 Knex 与 Posgresql 一起使用,我刚刚以这种方式解决了类似的问题:

    import * as Knex from "knex";
    
    export async function up(knex: Knex): Promise<void> {
        return await knex.schema
            .withSchema('public')
            .table('elements', (table) => {
                table.dropUnique(null, 'elements_name_unique');
            });
    }
    
    export async function down(knex: Knex): Promise<void> {
        return knex.schema.table('elements', (table) => {
            table.unique(['name']);
        });
    }
    

    Typescript 约束让它变得很奇怪,我不得不用参数作弊。

    我使用 Knex CLI 版本:0.21.17 / Knex 本地版本:0.21.18

    【讨论】:

      【解决方案2】:
      export async function down(db: Knex): Promise<void> {
          db.schema.alterTable('tablename', function (t) {
              t.dropUnique([], 'indexname')
          })
      }
      

      如果您使用打字稿删除索引。

      【讨论】:

        【解决方案3】:

        "knex": "^0.95.8",
        

        这是我通常做的:

        exports.up = function (knex) {
          return knex.schema.alterTable('users', function (table) {
            table.dropUnique('user_id');
          });
        };
        exports.down = function (knex) {
          return knex.schema.alterTable('users', function (table) {
            table.unique('user_id');
          });
        };
        

        但是我们遇到了一个有趣的问题,我们之前更改了列名并重命名了该列。由于某种原因,数据库仍然有一个约束,旧的列名是唯一的。所以我们必须先删除旧的约束。需要记住的一点!

        【讨论】:

          猜你喜欢
          • 2020-10-01
          • 2013-09-02
          • 2016-07-06
          • 1970-01-01
          • 2010-12-29
          • 2019-10-23
          • 2018-03-25
          • 2018-12-02
          • 2014-11-05
          相关资源
          最近更新 更多