question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Manually created constraint is dropped (and not recreated) by "typeorm migration:generate"

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.15 (or put your version here)

Steps to reproduce or a small repository showing the problem:

Here is first migration created by me manually (adding a constraint to a table - not index):

import { MigrationInterface, QueryRunner } from 'typeorm';

export class officeHoursNoOverlappingUserSessions1552041979708
  implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query('create extension if not exists btree_gist');
    await queryRunner.query(
      `alter table office_hours add constraint no_overlapping_user_time_ranges EXCLUDE USING GIST (user_id WITH =, time_range WITH &&)`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query(
      `alter table office_hours drop constraint no_overlapping_user_time_ranges`,
    );
  }
}

Next I added new value/option to one of enums and ran typeorm migration:generate. Here is the result:

import {MigrationInterface, QueryRunner} from "typeorm";

export class test1554716327860 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query(`ALTER TABLE "office_hours" DROP CONSTRAINT "no_overlapping_user_time_ranges"`);
        await queryRunner.query(`ALTER TYPE "exchange_whales_type_enum" RENAME TO "exchange_whales_type_enum_old"`);
        await queryRunner.query(`CREATE TYPE "exchange_whales_type_enum" AS ENUM('limitOrder', 'marketOrder', 'abc')`);
        await queryRunner.query(`ALTER TABLE "exchange_whales" ALTER COLUMN "type" TYPE "exchange_whales_type_enum" USING "type"::"text"::"exchange_whales_type_enum"`);
        await queryRunner.query(`DROP TYPE "exchange_whales_type_enum_old"`);
    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query(`CREATE TYPE "exchange_whales_type_enum_old" AS ENUM('limitOrder', 'marketOrder')`);
        await queryRunner.query(`ALTER TABLE "exchange_whales" ALTER COLUMN "type" TYPE "exchange_whales_type_enum_old" USING "type"::"text"::"exchange_whales_type_enum_old"`);
        await queryRunner.query(`DROP TYPE "exchange_whales_type_enum"`);
        await queryRunner.query(`ALTER TYPE "exchange_whales_type_enum_old" RENAME TO "exchange_whales_type_enum"`);
        await queryRunner.query(`ALTER TABLE "office_hours" ADD CONSTRAINT "no_overlapping_user_time_ranges" EXCLUDE USING gist (user_id WITH =, time_range WITH &&)`);
    }

}

As you can see migration commands regarding enum are correct, but for some reason typeorm drops constraint created manually in previous migration and it does not recreate it at the end of the migration:

await queryRunner.query(`ALTER TABLE "office_hours" DROP CONSTRAINT "no_overlapping_user_time_ranges"`);

Typeorm should not drop manually created constraint or it should drop it and recreate at the end of migration.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Kononnablecommented, Apr 23, 2019

#3904 defferable has been implemented recently(isn’t published yet) But I think I get what you’re asking for - you want to have ability to not synchronize specific checks. I will add a discussion label - maybe someone comes up with a idea how to do it nicely(add synhronize:false to specyfic decorators options, implement constraint decorator etc.). There are few ways how to implement it, we should find the best one.

0reactions
alekbarszczewskicommented, Apr 23, 2019

In this case it could work but I have other constraints which make use of postgres specific statements, for example deferrable initially deferred and also indexes/constraints on expressions like lower(column).

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM CLI: No changes in database schema were found
Can anyone tell me why is schema:log detecting the changes in my entities, but migration:generate is not working? migration · nestjs · typeorm....
Read more >
Migrations - typeorm - GitBook
This is useful for migrations created after manual changes have already been made to the database or when migrations have been run externally...
Read more >
Customize a migration file - Prisma
Create a draft migration using: $npx prisma migrate dev --create-only. Modify the generated SQL file. Apply the modified SQL by running:.
Read more >
Migrations Over Synchronize in TypeORM | by Uthpala Pitawela
This command will run the migration which you have already created in the above command. When you run this command, it will execute...
Read more >
TypeORM Migrations Explained - Better Programming
This guide builds upon a previous piece that showed how to quickly get started with TypeORM, MySQL, and ExpressJS. If you don't have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found