【问题标题】:typeORM No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" commandtypeORM 未发现数据库架构更改 - 无法生成迁移。要创建新的空迁移,请使用“typeorm migration:create”命令
【发布时间】:2021-08-02 00:24:27
【问题描述】:

我正在做一门课程,我们使用 express-node postgres 并做出反应(使用打字稿)和 typeORM。到目前为止一切都运行良好,但我遇到了 typeORM 的问题,我不知道发生了什么以及为什么会发生此错误

大问题:所以,问题在于,我正在执行身份验证,并且在某些时候我们更改了数据库中的用户架构,因此我们必须进行迁移,但是,在迁移之前,我们做了npm run typeorm schema:drop,但是当我尝试时,这发生在迁移

这是我使用的第一个命令

npm run typeorm migration:generate -- --n create-users-table

这是错误

> new-typeorm-project@0.0.1 typeorm C:\Users\diego cifuentes\Desktop\Studying  Backend\redit> ts-node ./node_modules/typeorm/cli.js "migration:generate" "create-users-table"

bin.js migration:generate

Generates a new migration file with sql needs to be executed to update
schema.

Opciones:
  -h, --help          Muestra ayuda                             [booleano]  -c, --connection    Name of the connection on which run a query.
                                                      [defecto: "default"]  -n, --name          Name of the migration class.
                                        [cadena de caracteres] [requerido]  -d, --dir           Directory where migration should be created.
  -p, --pretty        Pretty-print generated SQL
                                               [booleano] [defecto: false]  -f, --config        Name of the file with connection configuration.
                                                    [defecto: "ormconfig"]  -o, --outputJs      Generate a migration file on Javascript instead of
                      Typescript               [booleano] [defecto: false]      --dr, --dryrun  Prints out the contents of the migration instead of
                      writing it to a file     [booleano] [defecto: false]      --ch, --check   Verifies that the current database is up to date and                      that no migrations are needed. Otherwise exits with
                      code 1.                  [booleano] [defecto: false]  -v, --version       Muestra número de versión                 [booleano]
Falta argumento requerido: n
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! new-typeorm-project@0.0.1 typeorm: `ts-node ./node_modules/typeorm/cli.js "migration:generate" "create-users-table"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the new-typeorm-project@0.0.1 typeorm script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\diego cifuentes\AppData\Roaming\npm-cache\_logs\2021-05-11T15_29_02_081Z-debug.log

几个小时后试图到处寻找解决方案,我将最后一个命令更改为这个

npx typeorm migration:generate -- --n create-users-table

而且这次的错误不一样,看起来是这样的

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

所以,我说,好吧,我越来越接近解决这个问题了,但是......经过数小时(再次)寻找新答案后,我找不到任何东西,所以,我要说到点子上了我需要在哪里写这篇文章以解决我的问题。那么现在,让我向您展示我的 ormconfig、我的 package.json 以及我想要在我的 postgres 数据库中迁移的实体。

package.json(脚本typeorm在最后)

{
  "name": "new-typeorm-project",
  "version": "0.0.1",
  "description": "Awesome project developed with TypeORM.",
  "devDependencies": {
    "@types/bcrypt": "^5.0.0",
    "@types/cookie": "^0.4.0",
    "@types/cookie-parser": "^1.4.2",
    "@types/express": "^4.17.11",
    "@types/jsonwebtoken": "^8.5.1",
    "@types/morgan": "^1.9.2",
    "@types/node": "^15.0.2",
    "morgan": "^1.10.0",
    "nodemon": "^2.0.7",
    "ts-node": "^9.1.1",
    "typescript": "^4.2.4"
  },
  "dependencies": {
    "bcrypt": "^5.0.1",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "cookie": "^0.4.1",
    "cookie-parser": "^1.4.5",
    "dotenv": "^9.0.1",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.4.0",
    "reflect-metadata": "^0.1.10",
    "typeorm": "0.2.32"
  },
  "scripts": {
    "start": "ts-node src/server.ts",
    "dev": "nodemon --exec ts-node src/server.ts",
    "typeorm": "ts-node ./node_modules/typeorm/cli.js"
  }
}

ormconfing.json 快速说明:我将实体、迁移和订阅者更改为 .js,因为 .ts 总是给我错误。

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "**",
  "password": "**",
  "database": "**",
  "synchronize": true,
  "logging": true,
  "entities": ["src/entities/**/*.js"],
  "migrations": ["src/migrations/**/*.js"],
  "subscribers": ["src/subscribers/**/*.js"],
  "cli": {
    "entitiesDir": "src/entities",
    "migrationsDir": "src/migrations",
    "subscribersDir": "src/subscribers"
  }
}

最后我想告诉你的,虽然它可能不是那么重要,但这是用户架构

import {
  Entity as TOEntity,
  Column,
  Index,
  BeforeInsert,
  OneToMany
} from "typeorm";
import bcrypt from "bcrypt";
import { IsEmail, Length } from "class-validator";
import { Exclude } from "class-transformer";

import Entity from "./Entity";
// import Post from "./Post";

@TOEntity("users")
export default class User extends Entity {
  constructor(user: Partial<User>) {
    super();
    Object.assign(this, user);
  }

  @Index()
  @IsEmail()
  @Column({ unique: true })
  email: string;

  @Index()
  @Length(3, 200)
  @Column({ unique: true })
  username: string;

  @Exclude()
  @Length(6, 200)
  @Column()
  password: string;

  // @OneToMany(() => Post, post => post.user)
  // posts: Post[];

  @BeforeInsert()
  async hashedPassword() {
    this.password = await bcrypt.hash(this.password, 6);
  }
}

最终目标:所以,如果你能帮我解决这个问题,你会救我的命。最后一件事是我试图先在堆栈溢出的西班牙语网站上发布我的问题,但没有人可以帮助我,所以也许这里有人会,感谢您的时间并保重!

【问题讨论】:

    标签: node.js postgresql npm typeorm


    【解决方案1】:

    将您的entitiesmigrationssubscribers 更改为您的dist 目录。

    例如:

      entities: ["dist/entities/**/*{.js,.ts}"],
      migrations: ["dist/migrations/**/*{.js,.ts}"],
      subscribers: ["dist/subscribers/**/*{.js,.ts}"],
    

    dist 目录是您构建应用程序时创建的目录。您可以使用 tsconfig.json -> compilerOptions -> outDir 找到您的 dist 目录。

    然后使用npm run build 再次构建您的应用并尝试生成迁移。

    【讨论】:

    • 在我的 tsconfig 中。在 outDir 中出现 './build' 那么,我应该将其更改为 build 吗?实际上,想想我遇到的问题,我认为这可能是因为我将同步设置为真,当我进行迁移时,我应该将其设置为假?并感谢您回答我!
    • 是的,您应该将其更改为 build 并将同步设置为 false
    • 好的,谢谢你的回答!!
    • 我进行了更改,我首先使用 npm run typeorm,它给了我在帖子中提到的初始错误,然后,我尝试使用 npx,它出现相同的消息,没有更改在数据库模式中找到 - 无法生成迁移。要创建新的空迁移,请使用“typeorm migration:create”命令。我不明白:/
    • 这是因为您之前已经同步为true,因此数据库已经与您的实体类保持同步。您需要做的是删除数据库,创建一个新的同名空数据库,然后尝试生成迁移。如果这是您第一次创建迁移,那么您将生成迁移以从头开始创建所有表。如果您需要数据库中的数据,但仍想创建迁移,则该过程有点复杂。因此,如果是这种情况,请在此处发表评论。我只是给了你最简单的解决方案
    【解决方案2】:

    我看到了这个错误,如果我没记错的话,那是因为我试图创建一个已经创建的银行。

    下面的这个命令会删除所有的表:

    yarn typeorm query "DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT USAGE ON SCHEMA public to PUBLIC; GRANT CREATE ON SCHEMA public to PUBLIC; COMMENT ON SCHEMA public IS 'standard public schema';"

    那么:

    yarn typeorm --migration:generate -n migration_name -d path/to/your/migrations/

    【讨论】:

      【解决方案3】:

      对我来说 ormconfig.json 是这样的,

         "entities": [
            "dist/entity/**/*.ts"
         ],
         "migrations": [
            "src/migration/**/*.ts"
         ],
         "subscribers": [
            "src/subscriber/**/*.ts"
         ],
      

      改成

         "entities": [
            "src/entity/**/*{.js,.ts}"
         ],
         "migrations": [
            "src/migration/**/*{.js,.ts}"
         ],
         "subscribers": [
            "src/subscriber/**/*{.js,.ts}"
         ],
      

      正确生成迁移。

      而package.json脚本是这样的 "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"

      而使用的命令是

      npm run typeorm migration:generate -- -n FileAddTemporaryDeletedFields -p --dr --ch

      【讨论】:

        猜你喜欢
        • 2022-11-14
        • 2019-11-17
        • 2021-04-29
        • 2020-07-05
        • 2021-12-05
        • 2021-12-06
        • 1970-01-01
        • 2021-10-17
        • 2021-11-13
        相关资源
        最近更新 更多