Skip to content

Typeorm generating migrations even though there is not any change in models #7955

Closed as not planned

Description

@chandrashekhar07

Issue Description

i created a passenger.entity.ts file added few fields over it.
Then i generated a migration named first and i run migration run command successfully and then i can feel changes to my PostgreSQL database,

after few minutes, again i generated a migration named second,

here i am surprised to see that the second migration even generate the same SQL query as of first migrations.

Expected Behavior

i believe when i run command to generated second migration, it should show "typeORM No changes in database schema were found - cannot generate a migration. To create a new empty migration use “typeorm migration:create” command "

Actual Behavior

when i run first migration, following code is generated:

import {MigrationInterface, QueryRunner} from "typeorm";

export class first1627232991461 implements MigrationInterface {
    name = 'first1627232991461'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "passengers" ALTER COLUMN "is_deleted" SET DEFAULT 'false'`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "passengers" ALTER COLUMN "is_deleted" SET DEFAULT false`);
    }

}

when i run second migration command, following code is genetated:

import {MigrationInterface, QueryRunner} from "typeorm";

export class second1627234626276 implements MigrationInterface {
    name = 'second1627234626276'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "passengers" ALTER COLUMN "is_deleted" SET DEFAULT 'false'`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "passengers" ALTER COLUMN "is_deleted" SET DEFAULT false`);
    }

}

Surprisingly in both case there is same sql query

Steps to Reproduce

name of my column is inside .entity.ts file is:

@Column({ name: 'is_deleted', type: 'boolean', default: 'false' })
public isDeleted: boolean;

My Environment

Dependency Version
Operating System Ubuntu 20.04
Node.js version 14.16.1
Typescript version 4..2.4
TypeORM version 0.2.32

Additional Context

i believe following image clearly show this issue

image

Relevant Database Driver(s)

DB Type Reproducible
postgres yes

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Activity

  1. imnotjames commented on Jul 26, 2021

    @imnotjames
    Contributor

    Best guess without digging into it:

    The false is normalized by the postgres database server to be FALSE which FALSE !== false so it generates a new one.

  2. chandrashekhar07 commented on Jul 26, 2021

    @chandrashekhar07
    Author

    @imnotjames , i tried changing default value to FALSE, but still same sql query is regenerated,

  3. denvolok commented on Jul 29, 2021

    @denvolok
  4. imnotjames commented on Jul 29, 2021

    @imnotjames
  5. RS-DU34 commented on Aug 23, 2021

    @RS-DU34

    it's not clear on your post, looks like you didn't....
    ; did you actually run the migration or just the generate command?

    the generate command just generates code by looking at the db. If you dont use the run command, the db is unchanged and the next generate will find the db in the same state 😊

  6. chandrashekhar07 commented on Aug 23, 2021

    @chandrashekhar07
    Author

    @lflfm i have run the migration command (after generating it),
    and even after running the migration command,
    Migration:generate command is generating migration files which contains same SQL query as of first migration.

  7. RS-DU34 commented on Aug 23, 2021

    @RS-DU34

    which migration command? did you run migration:run ?
    correct order is:
    typeorm-cli migration:generate -n mynewChanges
    typeorm-cli migration:run
    typeorm-cli migration:generate -n someMoreChanges

  8. chandrashekhar07 commented on Aug 23, 2021

    @chandrashekhar07
    Author

    @lflfm yeah i know the correct order and i have followed the same order (you can check on photo attached above, i have merged generate and run command with && )
    here,
    typeorm-cli migration:generate -n someMoreChanges is generating same sql files as of typeorm-cli migration:generate -n mynewChanges

  9. RS-DU34 commented on Aug 23, 2021

    @RS-DU34

    Sorry, I hadn't read the command completely.
    The only thing I notice now then is that the migrate up is trying to set default to a string that says 'false' while the migrate down sets the default to a boolean false. Maybe that's the issue?
    Have you tried to set the default as a boolean in your model?
    instead of:
    @Column({ name: 'is_deleted', type: 'boolean', default: 'false' })
    try:
    @Column({ name: 'is_deleted', type: 'boolean', default: false })

  10. chandrashekhar07 commented on Aug 23, 2021

    @chandrashekhar07
    Author

    @lflfm Thank u man, setting default:false solves the issue

  11. forrestwilkins commented on Jul 25, 2023

    @forrestwilkins

    I'm getting a similar issue to this. I get the following SQL generated, even though no changes have been made to the GroupMemberRequest entity in ages:

    await queryRunner.query(
      `CREATE SEQUENCE IF NOT EXISTS "group_member_request_id_seq" OWNED BY "group_member_request"."id"`
    );
    await queryRunner.query(
      `ALTER TABLE "group_member_request" ALTER COLUMN "id" SET DEFAULT nextval('"group_member_request_id_seq"')`
    );
    await queryRunner.query(
      `ALTER TABLE "group_member_request" ALTER COLUMN "id" DROP DEFAULT`
    );
    

    I've cleared out my dist folder and also tried recreating my local dev database with no luck.

  12. forrestwilkins commented on Jul 25, 2023

    @forrestwilkins

    Has anyone tried the solution found here by chance?

  13. CodySwannGT commented on Nov 9, 2023

    @CodySwannGT

    Having the same issue

  14. sjlehn commented on May 17, 2024

    @sjlehn

    Has anyone tried the solution found here by chance?

    I just did. It didn't help. I still have a pile of bogus migrations that re-create a handful of indexes and foreign keys, as well as some regular fields. There's no difference in what's in up vs what's in down. When I dig into the debugger, it looks like it thinks the FKs should be cascading when they're not (in node_modules/typeorm/schema-builder/RdbmsSchemaBuilder.js::dropOldForeignKeys), though the generated update shows NO ACTION, which matches the existing table.

    Some of the fields being re-created are shared by 40 some tables (they're history fields - createdAt, modifiedAt, etc), yet only 3 tables are getting fake migrations generated - and by shared I mean they all exist on the same superclass. It's the exact same code that somehow triggers a migration for 3/49 entities. I can't figure out what makes those particular tables unique.

  15. zedjarvis commented on Jul 26, 2024

    @zedjarvis

    I realized that the migration:generate will re-create the same migration only if you have not executed migrations:run. After running migration then try to re-generate without changes you will get the following message No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command

  16. sjlehn commented on Jul 26, 2024

    @sjlehn

    Our team found the source of the additional migrations in our project; it was a configuration issue. We were defining ManyToMany relationships multiple times for a single relation, once with @ManyToMany from entity to entity, and again with @JoinTable on both sides that pointed to the junction table. This resulted in TypeORM creating two entities for every relation, and any discrepancy between the two caused the additional migrations. If you're defining manual junction tables via @JoinTable (b/c you need additional data there), you can't use @ManyToMany, and vice-versa.

  17. nojcc commented on Aug 10, 2024

    @nojcc

    Years later, this is still an issue smh

  18. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions