本文发布于1067天前,最后更新于 1067 天前,其中的信息可能已经有所发展或是发生改变。
问题复现
运行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






