typeorm 迁移 api 不会自动生成代码 [英] typeorm migration api does not generate automatic code

查看:42
本文介绍了typeorm 迁移 api 不会自动生成代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在自动生成迁移代码时遇到问题.基本上我在更改列时会遵循一些教程,迁移代码是自动生成的,但它不会发生在我身上.

hello I have a problem to generate the code of my migrations automatically. basically i following some tutorials when changing a column the migration code is generated automatically, but it doesn't happen to me.

我在 package.json 上的脚本:

my script on 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"
  },

我的实体(我将随机列更改为其他名称以测试使用自动代码生成迁移)

my entitiy ( i change random column to other name to test generate migration with automatic code)

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:

and this is my 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`,
  },
};

我启动了我的服务器,将列密码更改为密码并执行命令:

I started my server, changed the column password to passwords and executed the command:

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.

但由于某种原因,它没有在迁移中自动生成变更表:

but for some reason it is not generating the alter table automatically in the migration:

export class Password1595290975334 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
    }

}

推荐答案

尝试添加到 package.json :

"scripts": {
   ...
   "migration:generate": "yarn run typeorm migration:generate -n"
}

并执行此命令:

yarn run migration:generate -- [MigrationNameWithoutBrackets]

这篇关于typeorm 迁移 api 不会自动生成代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆