【发布时间】:2020-08-12 15:01:27
【问题描述】:
我正在使用 NestJS 和 TypeORM 开发应用程序。每当我尝试从我的实体生成迁移(通过运行 typeorm migration:generate)时,我都会收到以下消息:
No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command
我什至删除了所有现有的迁移文件(从迁移文件夹中)以排除任何冲突的可能性。
我将应用程序的模块文件夹结构更改为:
src
|- config
| |- database
| |- mysql
| |- cli-configuration.ts
|- migrations
|- module1
|- controller
|- entity
|- service
可以看出,migrations 文件夹就在 src 下方,每个模块都有一个 entity 文件夹,用于放置该模块的实体。所有的 ormconfig 设置都来自cli-configuration.ts 文件。
在package.json 文件中,我将以下内容添加到scripts:
{
...
"scripts": {
...
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/config/database/mysql/cli-configuration.ts",
"typeorm:migrate": "npm run typeorm migration:generate -- -n",
"typeorm:run": "npm run typeorm migration:run"
}
}
src/config/database/mysql/cli-configuration.ts文件的内容是:
import * as path from 'path';
import * as dotenv from 'dotenv';
dotenv.config({
// Path relative to project root folder (because cli command is invoked from there)
path: path.resolve('environment', (process.env.NODE_ENV === "production") ? ".env.production" : ".env")
});
const config = {
type: "mysql",
host: process.env.TYPEORM_HOST,
port: Number(process.env.TYPEORM_PORT),
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
entities: [path.resolve('src', process.env.TYPEORM_ENTITIES)],
// We are using migrations, synchronize should be set to false.
synchronize: false,
// Run migrations automatically,
// you can disable this if you prefer running migration manually.
migrationsRun: true,
logging: process.env.TYPEORM_LOGGING,
// Allow both start:prod and start:dev to use migrations
// __dirname is either dist or src folder, meaning either
// the compiled js in prod or the ts in dev.
migrations: [path.resolve('src', process.env.TYPEORM_MIGRATIONS)],
cli: {
// Location of migration should be inside src folder
// to be compiled into dist/ folder.
// entitiesDir: process.env.TYPEORM_ENTITIES_DIR,
migrationsDir: path.resolve('src', process.env.TYPEORM_MIGRATIONS_DIR),
// subscribersDir: process.env.TYPEORM_SUBSCRIBERS_DIR,
},
dropSchema: false
};
export = config;
运行console.log(config) 我明白了:
{
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'myapp',
entities: [ '/var/www/html/myapp/src/**/*.entity{.ts,.js}' ],
synchronize: false,
migrationsRun: true,
logging: 'all',
migrations: [ '/var/www/html/myapp/src/migrations/**/*{.ts,.js}' ],
cli: { migrationsDir: '/var/www/html/myapp/src/migrations' },
dropSchema: false
}
最后但并非最不重要的......在这个问题上花了几个小时(更改迁移和实体路径,硬编码值,除了从 process.env 中获取它们等)后,我尝试了执行yarn run typeorm schema:log(npm run 也可以)。当我看到我试图生成的迁移文件中的所有内容都输出到控制台时,我感到很惊讶。
CREATE TABLE `permission_permission` (`id` varchar(36) NOT NULL, `name` varchar(100) NOT NULL, `code` text NOT NULL, `create_date` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `update_date` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `delete_date` datetime(6) NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
CREATE TABLE `permission_role` (`id` varchar(36) NOT NULL, `name` varchar(100) NOT NULL, `code` varchar(255) NOT NULL, `create_date` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `update_date` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `delete_date` datetime(6) NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
CREATE TABLE `user_user` (`id` varchar(36) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `first_name` varchar(255) NOT NULL, `last_name` varchar(255) NOT NULL, `is_active` tinyint NOT NULL DEFAULT 0, `create_date` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `update_date` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `delete_date` datetime(6) NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
CREATE TABLE `permission_role_permission` (`permission_id` varchar(36) NOT NULL, `role_id` varchar(36) NOT NULL, INDEX `IDX_3247040996395a5faea3c9b3a5` (`permission_id`), INDEX `IDX_7d9cfbfd027256ab08658bcf6e` (`role_id`), PRIMARY KEY (`permission_id`, `role_id`)) ENGINE=InnoDB;
CREATE TABLE `user_user_role` (`role_id` varchar(36) NOT NULL, `user_id` varchar(36) NOT NULL, INDEX `IDX_c0c2bbb31e8e8708efc6dd5a64` (`role_id`), INDEX `IDX_beb8c39c852f4d132ba44b483c` (`user_id`), PRIMARY KEY (`role_id`, `user_id`)) ENGINE=InnoDB;
ALTER TABLE `permission_role_permission` ADD CONSTRAINT `FK_3247040996395a5faea3c9b3a54` FOREIGN KEY (`permission_id`) REFERENCES `permission_role`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE `permission_role_permission` ADD CONSTRAINT `FK_7d9cfbfd027256ab08658bcf6e1` FOREIGN KEY (`role_id`) REFERENCES `permission_permission`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE `user_user_role` ADD CONSTRAINT `FK_c0c2bbb31e8e8708efc6dd5a64b` FOREIGN KEY (`role_id`) REFERENCES `user_user`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE `user_user_role` ADD CONSTRAINT `FK_beb8c39c852f4d132ba44b483c0` FOREIGN KEY (`user_id`) REFERENCES `permission_role`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
谁能告诉我为什么schema:log 检测到我的实体的变化,但migration:generate 不起作用?
【问题讨论】:
-
我也有同样的问题。但是 schema:log 说“您的架构是最新的......”
-
我遇到了完全相同的问题。
-
您找到解决方案了吗?我也面临同样的问题,但运气不佳。