为什么 Sequelize 迁移不能与公共模式以外的模式正常工作?

发布于 2025-01-12 09:38:46 字数 1886 浏览 1 评论 0原文

我正在使用 Sequelize CLI 来执行迁移,但它似乎与“公共”之外的模式配合得不好。我运行了一系列测试,看来即使我在 Sequelize 命令的“选项”中提供架构,它也只适用于某些而不适用于其他。

例如,我可以在“向上”部分创建一个表,它最终会出现在正确的模式中,但在“向下”部分会失败,因为它无法公开找到该表:

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.createTable("migration_test", {
            name: Sequelize.DataTypes.STRING,
            isBetaMember: {
                type: Sequelize.DataTypes.BOOLEAN,
                defaultValue: false,
                allowNull: false
            }
        }, {
            schema: "profile"
        });
    },
    down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable("migration_test", {
            schema: "profile"
        });
    }
};

该表已正确创建:

执行(默认):CREATE TABLE IF NOT EXISTS “个人资料”。“migration_test”(“名称”VARCHAR(255),“isBetaMember” BOOLEAN NOT NULL DEFAULT false);

然而,关于“向下”:

执行(默认):DROP TABLE IF EXISTS "migration_test";

该表显然没有被删除,所以不是很有用。 SequelizeMeta 表最终以“公共”模式结束,如果必须的话,我可以接受它。

我也尝试了 addColumn,但“schema”属性甚至无法识别:

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.addColumn(
            "migration_test", 
            "test_column_1", 
            Sequelize.STRING(100), {
                schema: "profile"
            }
        );
    },
    down: (queryInterface, Sequelize) => {
        return queryInterface.removeColumn("migration_test", "test_column_1", {
            schema: "profile"
        });
    }
};

...抛出错误:

执行(默认):ALTER TABLE "public"."migration_test" ADD COLUMN “test_column_1”VARCHAR(100);

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

我做错了什么吗,或者这只是事情的状态?这似乎不可思议,这不是为了处理 pg 模式而构建的?与使用此框架相比,使用 db-migrate 之类的工具或仅编写一次性 SQL 脚本来进行数据库更改是否更好?

I'm using the Sequelize CLI to perform migrations, and it doesn't seem to play nice with schemas, outside of "public". I ran a series of tests, and it appears that even when I provide the schema in the "options" of the Sequelize command, it only works on some and not others.

For example, I can create a table on the "up" portion and it ends up in the proper schema, but fails on the "down" because it can't find the table in public:

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.createTable("migration_test", {
            name: Sequelize.DataTypes.STRING,
            isBetaMember: {
                type: Sequelize.DataTypes.BOOLEAN,
                defaultValue: false,
                allowNull: false
            }
        }, {
            schema: "profile"
        });
    },
    down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable("migration_test", {
            schema: "profile"
        });
    }
};

The table is created correctly:

Executing (default): CREATE TABLE IF NOT EXISTS
"profile"."migration_test" ("name" VARCHAR(255), "isBetaMember"
BOOLEAN NOT NULL DEFAULT false);

However, on "down":

Executing (default): DROP TABLE IF EXISTS "migration_test";

The table obviously isn't deleted, so not super useful. The SequelizeMeta table ends up in "public" schema, which I can live with, if I have to.

I tried addColumn as well, and the "schema" attribute isn't even recognized:

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.addColumn(
            "migration_test", 
            "test_column_1", 
            Sequelize.STRING(100), {
                schema: "profile"
            }
        );
    },
    down: (queryInterface, Sequelize) => {
        return queryInterface.removeColumn("migration_test", "test_column_1", {
            schema: "profile"
        });
    }
};

...throws an error:

Executing (default): ALTER TABLE "public"."migration_test" ADD COLUMN
"test_column_1" VARCHAR(100);

ERROR: relation "public.migration_test" does not exist

Am I doing something wrong, or is this just the state of things? It seems inconceivable that this wasn't built to handle pg schemas? Am I better off using something like db-migrate or just writing one-off SQL scripts for db changes, than using this framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

数理化全能战士 2025-01-19 09:38:46

如果模式不是 public,最好明确指示模式:

const table = { schema: SchemaName, tableName: TableName };

module.exports = {
    up: (queryInterface: QueryInterface): Promise<void> => queryInterface.sequelize.transaction(
        async (transaction) => {
            await queryInterface.createTable(table, attributes, { transaction });
        }
    ),

    down: (queryInterface: QueryInterface): Promise<void> => queryInterface.sequelize.transaction(
        async (transaction) => {
            await queryInterface.dropTable(table as any, { transaction });
        }
    )

It's better to indicate a schema explicitly if it's other than public:

const table = { schema: SchemaName, tableName: TableName };

module.exports = {
    up: (queryInterface: QueryInterface): Promise<void> => queryInterface.sequelize.transaction(
        async (transaction) => {
            await queryInterface.createTable(table, attributes, { transaction });
        }
    ),

    down: (queryInterface: QueryInterface): Promise<void> => queryInterface.sequelize.transaction(
        async (transaction) => {
            await queryInterface.dropTable(table as any, { transaction });
        }
    )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文