Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM's migration:generate regenerates the whole database schema

Tags:

typeorm

Every time I run migration:generate, it creates a migration that regenerates the entire database schema (rather than a migration for just the recent changes to my entities). I'm using Typeorm version 0.2.7, the latest version.

My ormconfig.json is:

{
  "host": "localhost",
  "logging": false,
  "port": 5432,
  "synchronize": false,
  "type": "postgres",

  "entities": ["build/server/entity/*.js"],
  "migrations": ["build/server/migration/*.js"],

  "cli": {
    "entitiesDir": "build/server/entity",
    "migrationsDir": "build/server/migration"
  },

  "username": "***",
  "password": "***",
  "database": "***"
}

When I run typeorm migration:generate -n SomeEntityChanges, the new migration file contains instructions for creating and linking up tables for all my entities, even though most of them already have corresponding migrations in build/server/migration.

When I run typeorm migration:run, I can see that there are no pending migrations, and that the migrations that cover the existing entities have been run (i.e. they're in my migrations table).

What am I missing? The docs say that the migration:generate command should just generate a migration with the recent changes.

like image 884
Nicholas Barry Avatar asked Oct 14 '18 06:10

Nicholas Barry


2 Answers

This might sound really stupid but I was having the same issue and the problem was the name of my database. My database name was mydbLocal with a capital L, but when typeorm was reading in order to generate a new migration it would look for mydblocal and since there was no schema with that name it caused the generation to regenerate the whole schema. It seems like it is a bug because on the parsing of the schema it looked for the lower case one but when running the migration it went into the real one (upper case L).

Anyway the way I solved it was by changing my db name to all lowercase and also editing my ormconfig database name to be all lowercase.

It is a really strange situation but this solved my problem. Hopefully it will help someone else too.

like image 58
Juan Casian Avatar answered Oct 28 '22 13:10

Juan Casian


As mentioned is one of the previous answers, the issue for me was indeed camel-casing the database name. Changing database name to all lowercase seems to have fixed the migration generate issue.

However, in my new project, I noticed that the entity table name override also seems to have the same behavior. Strangely, this was not an issue in the previous projects for me.

//Previous table name causing migration file to regenerate    
@Entity({
      name: 'TempTable',
    })

//New table name which stops the regeneration   
@Entity({
      name: 'temp_table',
    })

Hope this helps someone facing the same issue.

like image 31
Surya Avatar answered Oct 28 '22 15:10

Surya