gpt4 book ai didi

node.js - Sequelize addColumn 迁移始终引用公共(public)架构

转载 作者:行者123 更新时间:2023-12-03 22:18:25 24 4
gpt4 key购买 nike

我有一个带有 的 Express 应用程序 Sequelize 作为 ORM 和 PostgreSQL 作为数据库。
数据库的设置方式是,我的应用程序中的每个租户都有不同的架构。我的应用程序中存在的迁移文件包含 addColumn/removeColumn 迁移。
但是当我运行 npx sequelize-cli db:migrate 命令时,我收到以下错误。

错误:关系“public.table_name”不存在

上述错误仅针对包含 addColumn/removeColumn 迁移的迁移文件。
我也没有提到公共(public)模式(甚至从数据库中删除了公共(public)模式)。
有没有办法在 Sequelize 中针对特定模式(比如 test_schema)运行迁移,而无需在迁移文件中硬编码模式名称?

更新#2

'use strict';

module.exports = {
up: async(queryInterface, Sequelize) => {
try {
await queryInterface.addColumn('table_name', 'new_field_name', {
type: Sequelize.INTEGER
});
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
},

down: async(queryInterface, Sequelize) => {
try {
await queryInterface.removeColumn('table_name','new_field_name');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
}
};

以上是 addColumn 迁移的代码。

最佳答案

您可以像这样使用 addColumn/removeColumn 的扩展语法:

const { tableSchemas } = require('../config.json')
const tableName = 'table_name'
...
module.exports = {
up: async(queryInterface, Sequelize) => {
// adding transaction because we have several changes
await queryInterface.sequelize.transaction(async transaction => {
for (const tableSchema of tableSchemas) {
const table = { schema: tableSchema, tableName: tableName }
await queryInterface.addColumn(table, 'new_field_name', {
type: Sequelize.INTEGER
}, { transaction });
}
})
},

down: async(queryInterface, Sequelize) => {
// adding transaction because we have several changes
await queryInterface.sequelize.transaction(async transaction => {
for (const tableSchema of tableSchemas) {
const table = { schema: tableSchema, tableName: tableName }
await queryInterface.removeColumn(table,'new_field_name', { transaction });
}
})
}
};

关于node.js - Sequelize addColumn 迁移始终引用公共(public)架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61477933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com