本文发布于 2022-11-29, 最后更新于 1367 天前,其中的信息可能已经有所发展或是发生改变。

文章摘要

问题复现 运行 pnpm typeorm migration:run 时报了如下错误 Error during migration run: Error: Unable to open file: "F:\Developer WorkDir\database.db". Unexpected identifier at Function.loadDataSource (C:\Users\Styunl…

问题复现

运行pnpm typeorm migration:run时报了如下错误

subunit (Auto identified)
Error during migration run: Error: Unable to open file: "F:Developer WorkDirdatabase.db". Unexpected identifier at Function.loadDataSource (C:UsersStyunlenAppDataLocalpnpmglobal5.pnpmtypeorm@0.3.10node_modulestypeormcommandsCommandUtils.js:22:19) at async Object.handler (C:UsersStyunlenAppDataLocalpnpmglobal5.pnpmtypeorm@0.3.10node_modulestypeormcommandsMigrationRunCommand.js:41:26)

解决

查阅官方资料发现是因为typeorm官方弃用了ormconfig.json配置文件,转而使用

data-source.ts,其中官方的demo里,data-source.ts的内容如下:

dart (Auto identified)
import "reflect-metadata" import { DataSource } from "typeorm" import { User } from "./entity/User" export const AppDataSource = new DataSource({ synchronize: true, logging: false, entities: [User], migrations: [], subscribers: [], })

根据我自己的项目结构稍作修改:

nix (Auto identified)
import "reflect-metadata" import { DataSource } from "typeorm" export const AppDataSource = new DataSource({ type: "sqlite", database: "./database.db", synchronize: true, logging: true, entities: ["src/models/sqlite/**/*{.js,.ts}"], migrations: [], subscribers: [], })

再运行pnpm typeorm migration:run -d data-source.ts

pgsql (Auto identified)
> typeorm-ts-node-commonjs "migration:run" "-d" "data-source.ts" query: SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" = 'migrations' query: CREATE TABLE "migrations" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "timestamp" bigint NOT NULL, "name" varchar NOT NULL) query: SELECT * FROM "migrations" "migrations" ORDER BY "id" DESC No migrations are pendingding

九仞之行

严于律己,宽以待人,深自警省,讷言敏行

订阅
💬 评论 (0)

还没有评论呢 ~ 快来抢沙发吧 🛋️

编辑器加载中…