本文发布于 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 时报了如下错误 Error during migration run: Error: Unable to open file: "F:\Developer WorkDir\database.db". Unexpected identifier at Function.loadDataSource (C:\Users\Styunlen\AppData\Local\pnpm\global\5\.pnpm\typeorm@0.3.10\node_modules\typeorm\commands\CommandUtils.js:22:19) at async Object.handler (C:\Users\Styunlen\AppData\Local\pnpm\global\5\.pnpm\typeorm@0.3.10\node_modules\typeorm\commands\MigrationRunCommand.js:41:26) 解决 查阅官方资料发现是因为 typeorm 官方弃用了 ormconfig.json 配置文件,转而使用 data-source.ts ,其中官方的demo里, data-source.ts 的内容如下: 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: ], }) 根据我自己的项目结构稍作修改: 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: ], }) 注意,这里的指令新加了 -d data-source.ts 属性,因为新版typeorm貌似不会自动寻找datasource文件,得手动传入 再运行 pnpm typeorm migration:run -d data-source.ts 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问题复现
运行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: [],
})
注意,这里的指令新加了-d data-source.ts属性,因为新版typeorm貌似不会自动寻找datasource文件,得手动传入
再运行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
本文采用 CC BY-NC-SA 4.0 协议进行授权,如无注明均为原创,转载请注明出处。
标题:typeorm运行cli报错Unexpected identifier作者:九仞之行链接:http://dev.styunlen.cn/archives/post-1527.html
还没有评论呢 ~ 快来抢沙发吧 🛋️