0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TypeORM マイグレーション自動生成しようとしたら `No changes in database schema were found` というエラーが出る

Last updated at Posted at 2025-03-20

前提

  • Node.js: v22.13.1
  • TypeScript: Version 5.8.2
  • TypeORM: 0.3.21
  • ts-node: 10.9.2

事象

以下のコマンドを実行して、エラーが発生した:

npx typeorm-ts-node-commonjs migration:generate src/migration/CreateDrugTable -d src/data-source.ts

エラーメッセージ:

No changes in database schema were found - cannot generate a migration.
To create a new empty migration use "typeorm migration:create" command

原因

このエラーの主な原因は、synchronize: true が設定されていたことです。synchronize: true にすると、TypeORM はアプリケーション起動時にデータベーススキーマを自動で更新します。この設定により、エンティティの変更が自動的にデータベースに反映され、不要な変更がログとして残ります。そのため、マイグレーションの自動生成時に変更が見つからず、エラーが発生することがあります。

解決策

エラーを解決するためには、synchronize: false に設定を変更した上で、データベーススキーマをリセットすることが必要です。以下の手順で問題を解決できます。

手順

  1. data-source.ts の設定確認

    data-source.tssynchronize: true が設定されていた場合、まずこれを false に変更します。これにより、データベーススキーマの自動更新を無効にします。

    const AppDataSource = new DataSource({
      type: "mysql",
      host: "localhost",
      port: 3306,
      username: "root",
      password: "password",
      database: "mydb",
      synchronize: false,  // この設定が重要
      entities: [Drug, User],
      migrations: ["src/migration/**/*.ts"],
    });
    
  2. データベーススキーマをリセット

    次に、データベーススキーマをリセットします。synchronize: false の設定により、データベースの変更が自動的に反映されなくなりますが、既存のスキーマをリセットするためには手動でスキーマを削除します。

    npx typeorm schema:drop -d src/data-source.ts
    
  3. マイグレーションの再生成

    データベーススキーマをリセットした後、再度マイグレーションを生成します。

    npx typeorm-ts-node-commonjs migration:generate src/migration/CreateDrugTable -d src/data-source.ts
    

これで、マイグレーションが正常に生成されるはずです。

まとめ

No changes in database schema were found エラーが発生する場合、synchronize: true 設定が影響している可能性があります。この場合、以下の手順で問題を解決できます。

  1. synchronize: false に設定を変更。
  2. npx typeorm schema:drop -d src/data-source.ts でデータベーススキーマをリセット。
  3. 再度、マイグレーションを生成。

これで、マイグレーションの自動生成が正常に動作するようになります!🚀

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?