postgresql postgress和knex迁移中出现未知错误

rm5edbpk  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(125)

我尝试运行已创建的迁移,但出现未知错误。我对数据库的这种工作是新手。也许有人遇到过这个问题。迁移脚本:npx knex migrate:latest --knexfile ./app/database/knexfile.js。可能是knex配置的问题,配置中指定的路径是正确的。还没有使用Seeeds。
knexfile:

require('dotenv').config();

const db_user = process.env.DB_USER;
const db_password = process.env.DB_PASSWORD;
const db_name = process.env.DB_NAME;

/**
 * @type { Object.<string, import("knex").Knex.Config> }
 */
module.exports = {
    development: {
        client: 'postgresql',
        connection: {
            database: `${db_name}`,
            user: `${db_user}`,
            password: `${db_password}`,
        },
        pool: {
            min: 2,
            max: 10,
        },
        migrations: {
            tableName: 'knex_migrations',
            directory: './migrations',
        },
        seeds: {
            directory: './seeds',
        },
    },
};

迁移文件:

// eslint-disable-next-line node/exports-style
exports.up = (knex) => {
    return knex.schema
        .createTable('users', (table) => {
            table.uuid('id').primary();
            table.string('name');
            table.string('email');
            table.sting('password');
        })
        .createTable('watchLists', (table) => {
            table.uuid('id').primary();
            table
                .foreign('userId')
                .references('id')
                .inTable('users')
                .onDelete('CASCADE');
            table.string('name');
        })
        .createTable('films', (table) => {
            table.uuid('id').primary();
            table.string('name');
            table.string('rate');
        })
        .createTable('addedFilms', (table) => {
            table.uuid('id').primary();
            table
                .foreign('watchListId')
                .references('id')
                .inTable('watchList')
                .onDelete('CASCADE');
            table
                .foreign('filmId')
                .references('id')
                .inTable('films')
                .onDelete('CASCADE');
        });
};

// eslint-disable-next-line node/exports-style
exports.down = (knex) => {
    return knex.schema
        .dropTableIfExists('addedFilms')
        .dropTableIfExists('films')
        .dropTableIfExists('users')
        .dropTableIfExists('watchList');
};

错误:

������������ "undefined" �� ������ �������� ����������� (�� ������)
error: ������������ "undefined" �� ������ �������� ����������� (�� ������)
    at Parser.parseErrorMessage (D:\All-Pet-Projects\effective-app\evgeny-trubish-node\node_modules\pg-protocol\dist\parser.js:287:98)
    at Parser.handlePacket (D:\All-Pet-Projects\effective-app\evgeny-trubish-node\node_modules\pg-protocol\dist\parser.js:126:29)
    at Parser.parse (D:\All-Pet-Projects\effective-app\evgeny-trubish-node\node_modules\pg-protocol\dist\parser.js:39:38)
    at Socket.<anonymous> (D:\All-Pet-Projects\effective-app\evgeny-trubish-node\node_modules\pg-protocol\dist\index.js:11:42)
    at Socket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:199:23)
bis0qfac

bis0qfac1#

问题是我错误地导入了.env文件,我应该在config()中指定正确的路径

相关问题