簡體   English   中英

typeORM 未發現數據庫架構更改 - 無法生成遷移。 要創建新的空遷移,請使用“typeorm migration:create”命令

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

我正在做一門課程,我們使用 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,因為 with.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);
  }
}

最終目標:所以,如果你能幫助我,你會救我的命。 最后一件事是我試圖首先在堆棧溢出的西班牙語網站上發布我的問題,但沒有人可以幫助我,所以也許這里有人會,感謝您的時間並保重!

將您的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並嘗試生成遷移。

我已經看到了這個錯誤,如果我沒記錯的話,那是因為我試圖創建一個已經創建的銀行。

下面的這個命令刪除所有表:

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/

對我來說 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 運行 typeorm 遷移:生成 -- -n FileAddTemporaryDeletedFields -p --dr --ch

據我所知,ormconfig.json 在最新版本中已棄用。 但無論如何,當我使用舊版本時,我會分享我的解決方案。

生成時,typeorm 首先讀取 ormconfig.json 並登錄到 rdbms。 並在“數據庫的實際模式”和“作為 ormconfig.json [實體] 的模式文件”之間進行比較

因此,如果文件(寫在 ormconfig.json 中的“實體”處)位於 dist 文件夾中。 您必須編輯 that.js 文件。(不是 src 文件夾中的 .ts 文件)

但是,如果“實體”文件在 src 文件夾中為 ormconfig.json,請檢查 vscode 的“自動保存”功能是否打開。 如果您 'npm run start:dev' 在您編輯實體文件之前,可能已經修改了 db 的實際架構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM