0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

knex.js + postgresql migration メモ

Last updated at Posted at 2020-05-28

knex.js を postgresql で利用する際の migration について、こうやるといいんじゃないかな的な話題をまとめていきます。(随時更新予定)

bigint な primary key

64bit にしたい場合。

exports.up = function(knex) {
    return knex.schema
        .createTable('xxxx', function (table) {
            table.specificType('id', knex.raw('BIGSERIAL PRIMARY KEY'));
        });
};

enum の up / down

up / down の行き来によって enum 型定義だけ残るので強制的に消しに行ってます。

const dropTypeStmt = 'drop type if exists xxx_type';

exports.up = function(knex) {
    return knex.schema
        .raw(dropTypeStmt)
        .createTable('xxx', function (table) {
            table.enu('state', ['a', 'b'], { useNative: true, enumName: 'xxx_type' });
        });
};

exports.down = function(knex) {
    return knex.schema
        .raw(dropTypeStmt);
};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?