postgresql - 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} <---------------
还是有一种我错过的续集迁移的简单或正确方法?
解决方案
当您创建续集连接时,请在定义对象上说您的架构,如下所示:
const sequelize = new Sequelize(database, username, password,
{
host, dialect, port, omitNull: true,
define: {
schema: "your-schema"
timestamps: true,
underscored: true
},
...
})
推荐阅读
- flutter - 使用 AnimationController 使其第二次可见后无法为颤振小部件设置动画
- c++ - 如何在函数内打印到标准输出时分离可变参数模板参数
- javascript - Socket.io join() 什么都不做
- wso2 - wso2 api manager 2.6.0 版中的货币化
- c - 如何在我的简单 C 程序中修复这个奇怪的错误?
- javascript - 无法在客户端填充缩略图
- java - 如何在 Springboot 中返回 JSON 响应?
- sql-server - 用一个主键和一个复合键连接两个表
- python - (PIL) Image.paste 未按预期运行
- java - SpringBoot Admin - Kubernetes - 服务器覆盖客户端 URL