【问题标题】:Sequelize migration tool can not find schema defined in config.jsonSequelize 迁移工具找不到 config.json 中定义的架构
【发布时间】:2020-12-17 20:35:12
【问题描述】:

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

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

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

然后我尝试手动在 pgadmin 中创建架构。然后我看到我的 user 和 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} <---------------

或者我错过了一个简单或正确的迁移续集方法​​?

【问题讨论】:

  • 您可以创建一个脚本,该脚本可以在容器启动时作为对容器的命令执行,并且该脚本可以在 postgres 中预先创建架构。

标签: postgresql docker sequelize.js database-schema sequelize-cli


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-01-04
    • 2020-02-16
    • 2020-02-15
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多