typeorm运行cli报错Unexpected identifier
本文发布于781天前,最后更新于 781 天前,其中的信息可能已经有所发展或是发生改变。

问题复现

运行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
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Source: https://github.com/zhaoolee/ChineseBQB
Source: https://github.com/zhaoolee/ChineseBQB
Source: https://github.com/zhaoolee/ChineseBQB
颜文字
Emoji
小恐龙
花!
滑稽大佬
演奏
程序员专属
上一篇
下一篇