Тип создания knex миграции для типа enum thows уже существует - PullRequest
0 голосов
/ 11 февраля 2020

Я пытаюсь изменить столбец в таблице, чтобы изменить перечисление knex на собственные типы, чтобы воспользоваться системой типов Postgres, когда я выполняю миграцию, я получаю этот тип ошибки "request_type" already exists, любая идея, что происходит здесь?

export async function up(knex: Knex): Promise<any> {
  return knex.schema.alterTable('appointments', table => {
    table.enu('type', ['video', 'physical'], { useNative: true, enumName: 'request_type' }).alter();
  });
}

export async function down(knex: Knex): Promise<any> {
  return knex.schema
    .alterTable('appointments', table => {
      table.dropColumn('type');
    })
    .then(() => knex.raw('CREATE TYPE request_type AS ENUM("video", "physical")'))
    .then(() => knex.raw('drop type request_type'));
}

1 Ответ

1 голос
/ 12 февраля 2020

Похоже, в knex есть ошибка, из-за которой при создании таких столбцов дважды добавляется запрос создания типа.

https://runkit.com/embed/xqtl8p2knhi8

const Knex = require('knex');

const knex = Knex({
  client: 'pg',
});

knex.schema.alterTable('appointments', table => {
    table.enu('type', ['video', 'physical'], { useNative: true, enumName: 'request_type' }).alter();
}).toSQL()

/*
  Creates SQL:

0: Object {bindings: [], sql: "create type \"request_type\" as enum ('video', 'physical')"}
1: Object {bindings: [], sql: "create type \"request_type\" as enum ('video', 'physical')"}
2: Object {bindings: [], sql: "alter table \"appointments\" alter column \"type\" drop default"}
3: Object {bindings: [], sql: "alter table \"appointments\" alter column \"type\" drop not null"}
4: Object {bindings: [], …}
*/
...