我正在尝试使用 TypeOrm 生成迁移。当我更改实体时,它应该检测到此更改并生成新的迁移。
我收到以下错误消息:
未发现数据库架构发生更改 - 无法生成迁移。要创建新的空迁移,请使用“typeorm migration:create”命令
当我更改实体文件中的某些内容时,为什么会收到此错误消息?
我正在使用此命令来运行 TypeOrm:
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/typeormConfig.ts",
这是我的 typeormConfig.ts 文件:
import { ConnectionOptions } from "typeorm";
import * as path from "path";
import * as dotEnv from 'dotenv'
console.log(path.resolve(__dirname, 'entity/**.entity.ts'))
const result = dotEnv.config({ path: './ormconfig.env' });
if (result.error) {
throw result.error
}
const config: ConnectionOptions = {
type: 'mysql',
host: process.env.TYPEORM_HOST,
port: +process.env.TYPEORM_PORT,
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
//synchronize: true,
synchronize: false,
// migrationsRun: false,
entities: [path.resolve(__dirname, 'entity/**.entity.*')],
migrations: [path.resolve(__dirname, 'migration/**')],
cli: {
entitiesDir: "src/entity",
migrationsDir: "src/migration"
}
}
export default config;
像这样更新实体和迁移目录链接
entities: [__dirname + '/entity/**/*.entity.ts', __dirname + '/entity/**/*.entity.js'],
migrations: [__dirname + '/migration/**/*.ts', __dirname + '/migration/**/*.js'],
它对我有用并且应该有用。
"migrations": ["db-migrations/*{.ts,.js}"],
"cli": {
"migrationsDir": "db-migrations"
}
synchronize
并转到上一个。您的架构状态(运行项目 - 这将重置数据库更改)synchronize
并运行项目(以使其编译更改) - 它可能会运行失败,但没关系typeorm migration:generate -n TestMigration
就会成功从那里离开
synchronize
禁用,只需在运行之前编译项目migration:generate