NodeJS 由于模式和数据库中的不匹配而导致的创建typorm迁移的问题

guz6ccqo  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(127)

当我创建一个新的迁移而不做任何更改时,我正在处理的项目会创建这个新的迁移文件。这可能是直接在数据库中进行的更改,而没有正确更改模式。但是我很难用数据库中提供的名称创建外键约束,例如email_notifications_settings_droid_id_fkey,push_notifications_settings_account_id_fkey。这将是一个很大的帮助,如果有人能帮助我。谢谢你,谢谢!
我使用postgres作为数据库。这是新的迁移文件,没有对模式或数据库做任何更改

import {MigrationInterface, QueryRunner} from "typeorm";

export class asdf1696256327292 implements MigrationInterface {
    name = 'asdf1696256327292'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "devices_settings" DROP CONSTRAINT "devices_settings_account_id_fkey"`);
        await queryRunner.query(`ALTER TABLE "email_notifications_settings" DROP CONSTRAINT "email_notifications_settings_account_id_fkey"`);
        await queryRunner.query(`ALTER TABLE "email_notifications_settings" DROP CONSTRAINT "email_notifications_settings_droid_id_fkey"`);
        await queryRunner.query(`ALTER TABLE "push_notifications_settings" DROP CONSTRAINT "push_notifications_settings_account_id_fkey"`);
        await queryRunner.query(`ALTER TABLE "push_notifications_settings" DROP CONSTRAINT "push_notifications_settings_droid_id_fkey"`); 
        await queryRunner.query(`ALTER TABLE "map_settings" DROP CONSTRAINT "map_settings_droid_id_fkey"`); 
    }

    public async down(queryRunner: QueryRunner): Promise<void> { 
        await queryRunner.query(`ALTER TABLE "map_settings" ADD CONSTRAINT "map_settings_droid_id_fkey" FOREIGN KEY ("droid_id") REFERENCES "droids"("droid_id") ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE "push_notifications_settings" ADD CONSTRAINT "push_notifications_settings_droid_id_fkey" FOREIGN KEY ("droid_id") REFERENCES "droids"("droid_id") ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE "push_notifications_settings" ADD CONSTRAINT "push_notifications_settings_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "accounts"("account_id") ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE "email_notifications_settings" ADD CONSTRAINT "email_notifications_settings_droid_id_fkey" FOREIGN KEY ("droid_id") REFERENCES "droids"("droid_id") ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE "email_notifications_settings" ADD CONSTRAINT "email_notifications_settings_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "accounts"("account_id") ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE "devices_settings" ADD CONSTRAINT "devices_settings_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "accounts"("account_id") ON DELETE CASCADE ON UPDATE NO ACTION`);
    }

}

使用给定名称创建外键约束的方法

ekqde3dh

ekqde3dh1#

问题是外键是在数据库中创建的,而不是在代码中。我在代码中使用joins创建了它们,并添加了自定义命名策略来根据我的需要重命名这些键。

相关问题