postgresql - Knex 和 PostgreSQL : How to drop an unique index?

标签 postgresql knex.js

我有一个使用 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

有人可以帮助我/插入我朝着正确的方向前进吗?

最佳答案

将 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 Local 版本:0.21.18

关于postgresql - Knex 和 PostgreSQL : How to drop an unique index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64860705/

相关文章:

arrays - 在 Postgres JSON 数组中查询

c++ - 在 Windows 上连接到 postgresql

typescript - knex.js:表推断 TypeScript 类型

postgresql - Heroku PostgreSQL Crane DB 与安装了 PostgreSQL 的 Linode 1GB

PostgreSQL 和临时表触发器

postgresql - 未处理的拒绝 SequelizeForeignKeyConstraintError : insert or update on table

javascript - Knex插入多条记录只返回插入的第一条记录

node.js - Bookshelf js fetchAll withRelated 选项返回空关系对象

javascript - 如何在 Electron 的预加载js中运行knex?

sinon - 如何在Hapi/Lab测试中使用Sinon来消除knex调用?