Issues with creating typorm migrations due to mismatches in the schema and database

28 Views Asked by At

When I create a new migration without doing any changes the project I am working on creates this new migration file. This possibly the changes done directly into the database without changing the schema properly. But I am having difficulties to create foreign key constrains with the provided names in the DB such as email_notifications_settings_droid_id_fkey, push_notifications_settings_account_id_fkey. It would be a great help if anyone can help me with this. Thank you !

I am using postgres as the DB.This is the new migration file without doing any changes to the schema or DB

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`);
    }

}

A way to create foreign key constrains with the given name

1

There are 1 best solutions below

0
Nipuna On

The issue was foreign keys are created in the db and not in the code. I created them in the code using joins and added custom naming strategy to rename these keys as I want.