migration:generate does not automatically drop tables
See original GitHub issueIssue Description
When I create an entity, generate a migration, and run said migration, it creates a table for the entity in the mysql database as expected. When I delete the entity, and attempt to generate a new migration on top of the old one, the typeorm does not find any changes.
Expected Behavior
I expected typeorm to recognize that the entity did not exist and so the table could be dropped. Typeorm should have made a new migration to drop the table whose entity was deleted.
Actual Behavior
I made the following entity:
import {
Entity,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity()
export class Foo {
@PrimaryGeneratedColumn()
id: number;
}
I ran typeorm migration:generate -- -n addFoo
which created the following migration:
import {MigrationInterface, QueryRunner} from "typeorm";
export class addFoo1624984985251 implements MigrationInterface {
name = 'addFoo1624984985251'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("CREATE TABLE `foo` (`id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)) ENGINE=InnoDB");
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("DROP TABLE `foo`");
}
}
I ran typeorm migration:run
which ran the migration and updated the database as expected.
I deleted the file in which foo was defined.
I ran typeorm migration:generate -- -n removeFoo
add got the following output:
> api@0.0.1 typeorm /usr/src/app
> node --require ts-node/register ./node_modules/typeorm/cli.js "migration:generate" "-n" "removeFoo"
No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! api@0.0.1 typeorm: `node --require ts-node/register ./node_modules/typeorm/cli.js "migration:generate" "-n" "removeFoo"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the api@0.0.1 typeorm script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-06-29T16_41_52_271Z-debug.log
Steps to Reproduce
The entity and migrations above are the files in their entirety, so the entity can be used to reproduce.
- Create an entity
- Generate a migration
- Run migrations
- Delete entity
- Generate a migration
My Environment
| Dependency | Version | | Operating System | macOS Big Sur v11.4 | | Node.js version | v16.2.0 | | Typescript version | v4.2.4 | | TypeORM version | v0.2.32 |
Additional Context
I first found this issue when joining tables in many-to-many relationships were not being dropped automatically. This open issue also has this bug https://github.com/typeorm/typeorm/issues/5463 This issue occurs both for regular tables for declared entities and for joining tables. Typeorm has no problem automatically changing everything else such as foreign keys, column names, etc.
Relevant Database Driver(s)
-
mysql
Are you willing to resolve this issue by submitting a Pull Request?
- No, I don’t have the time and I wouldn’t even know how to start.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:16
- Comments:10
Top GitHub Comments
Same
If you have dev database, and can’t find previous migration that up and down the table.
First, I will directly drop table in dev database by using GUI or sql, then run
npm run migration:generate -n='RemoveArtistScheduleTable'
to create new migration. It will generate below queryRunner migration code.Second, manually run the sql to create table again. In this case, execute below sql to re-create table.
Finally, switch queryRunner method. Then delete your entity, associated code and run
npm run migration:run
on your dev and prod database.I know this is a little bit complicated, but I always do this way. Hope this can help.