首页 > 解决方案 > Sequelize 迁移工具找不到 config.json 中定义的架构

问题描述

当从 postgres 镜像启动一个 docker 容器时,它只在数据库下创建一个公共模式。这是我的设置

postgres:
        container_name: postgres
        image: postgres:latest
        ports:
            - "${DB_PORT}:5432"
        environment:
            - POSTGRES_USER=${DB_USER}
            - POSTGRES_PASSWORD=${DB_PASSWORD}
            - POSTGRES_DB=${DB_NAME}

我的解决方案是

// 111111-create-schema.js
await queryInterface.createSchema('custom');

// 222222-create-user.js
await queryInterface.createTable('Users', {}, {schema: 'custom'}

当我运行时,我确实在自定义模式npx sequelize-cli db:migrate中看到了我的用户表。但是,存储迁移的sequelizeMeta表位于PUBLIC shema中

我也尝试添加schema: 'custom'config.json。但是,当我运行 db:migrate 时,我得到了自定义模式未找到错误。我假设此时我的迁移脚本在尝试定位此自定义架构之前尚未运行。

然后我尝试在 pgadmin中手动创建模式。然后我看到我的用户和 sequelizeMeta 表都在这个自定义模式中。

我想知道如何让 postgres docker 容器预先创建一个模式,就像它使用 POSTGRES_DB 环境变量预先创建一个数据库一样?就像是

postgres:
        container_name: postgres
        image: postgres:latest
        ports:
            - "${DB_PORT}:5432"
        environment:
            - POSTGRES_USER=${DB_USER}
            - POSTGRES_PASSWORD=${DB_PASSWORD}
            - POSTGRES_DB=${DB_NAME}
            - POSTGRES_SCHEMA=${DB_SCHEMA} <---------------

还是有一种我错过的续集迁移的简单或正确方法?

标签: postgresqldockersequelize.jsdatabase-schemasequelize-cli

解决方案


当您创建续集连接时,请在定义对象上说您的架构,如下所示:

const sequelize = new Sequelize(database, username, password,
{
   host, dialect, port, omitNull: true,
   define: {
     schema: "your-schema"
     timestamps: true,
     underscored: true
   },
   ...
 })

推荐阅读