您好,我无法自动生成迁移代码。 基本上,我在更改列时会遵循一些教程,迁移代码是自动生成的,但是我不会发生。
我在package.json上的脚本:
"scripts": {
"commit": "git-cz",
"build": "babel src --extensions \".js,.ts\" --out-dir dist --copy-files --no-copy-ignored",
"start": "node dist/server.js",
"dev:server": "ts-node-dev --inspect --respawn --transpile-only --ignore-watch node_modules -r tsconfig-paths/register src/shared/infra/http/server.ts",
"test": "jest",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"lint": "eslint --fix"
},
我的想法(我将随机列更改为其他名称,以测试使用自动代码生成的迁移)
users.entitiy.ts:
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { SharedProp } from './sharedProp.helper';
@Entity({ name: 'users' })
export class User extends SharedProp {
constructor(isActive: boolean, login: string, password: string) {
super();
this.isActive = isActive;
this.login = login;
this.password = password;
}
@PrimaryGeneratedColumn()
id: number;
@Column({ name: 'login', nullable: false })
login: string;
@Column({ nullable: false })
password: string;
@Column({ name: 'is_active', nullable: false })
isActive: boolean;
}
这是我的ormconfig:
const rootDir = process.env.NODE_ENV === 'development' ? 'src' : 'build/src';
module.exports = {
type: 'postgres',
host: process.env.DB_HOST,
port: process.env.DB_PORT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
synchronize: false,
logging: false,
entities: [rootDir + '/entities/**/*.{js,ts}'],
migrations: [rootDir + '/migrations/*.{js,ts}'],
subscribers: [rootDir + '/subscribers/**/*.{js,ts}'],
seeds: [rootDir + '/migrations/seeds/**/*.{js,ts}'],
factories: [rootDir + '/migrations/factories/**/*.{js,ts}'],
cli: {
entitiesDir: `${rootDir}/entities`,
migrationsDir: `${rootDir}/migration`,
subscribersDir: `${rootDir}/subscriber`,
},
};
我启动了服务器,将列密码更改为password并执行了命令:
yarn typeorm -- migration:create -n Password
yarn run v1.22.4
warning From Yarn 1.0 onwards, scripts don't require "--" for options to
be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.
$ node --require ts-node/register ./node_modules/typeorm/cli.js migration:create -n Password
Migration G:\emasati_stockcontrol/src/migration/1595290975334-Password.ts has been generated successfully.
Done in 1.86s.
但是由于某种原因,它不会在迁移中自动生成alter table:
export class Password1595290975334 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}
答案 0 :(得分:2)
尝试添加到 package.json :
"scripts": {
...
"migration:generate": "yarn run typeorm migration:generate -n"
}
并执行此命令:
yarn run migration:generate -- [MigrationNameWithoutBrackets]