无法使用 migrationsRun: true 运行迁移。

huangapple go评论62阅读模式
英文:

Can't run migrations with migrationsRun: true

问题

我有一个React Native + Expo项目,其中设置了SQLite数据库和TypeORM。我正在尝试在应用程序启动时运行迁移,该迁移应向现有的Note表添加一个名为isPrivate的列,方法是将migrationsRun数据源配置选项设置为true。但是它不起作用。似乎TypeORM根本看不到迁移,即使路径是正确的。

如果Note实体定义了isPrivate列,而我尝试从存储库实例使用find方法,就会抛出以下错误:

没有此列: Note.isPrivate (code 1 SQLITE_ERROR):, 在编译时: SELECT "Note"."id" AS "Note_id", "Note"."title" AS "Note_title", "Note"."details" AS "Note_details", "Note"."isPrivate" AS "Note_isPrivate", "Note"."folderId" AS "Note_folderId" FROM "note" "Note"

这是data-source.ts文件:

import "reflect-metadata";
import { DataSource } from "typeorm";
import * as sqlitedriver from 'expo-sqlite';
import Folder from './entities/Folder.model.ts';
import Note from './entities/Note.model.ts';

const AppDataSource = new DataSource({
    driver: sqlitedriver,
    database: 'app_db',
    type: 'expo',
    migrationsRun: true,
    entities: [Folder, Note],
    migrations: ['migrations/*.ts'],
});

AppDataSource.initialize().then(() => {
    console.log("数据源已初始化!");
}).catch((err) => {
    console.error("数据源初始化期间出错", err.message);
});

export default AppDataSource;

migration文件:

import { MigrationInterface, QueryRunner } from "typeorm";
import { TableColumn } from "typeorm";

export class AddIsPrivateColumn1684782594104 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.addColumn(
            "Note",
            new TableColumn({
                name: "isPrivate",
                type: "boolean",
            }),
        );
    };

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.dropColumn("Note", "isPrivate");
    };
};

如何运行迁移?数据源配置有什么问题吗?

英文:

I have a React Native + Expo project with SQLite database and TypeORM set up. I'm trying to run a migration which should add an isPrivate column to an existing Note table on application start, by setting migrationsRun data source config option to true. But it doesn't work. It seems like TypeORM can't see migrations at all, even though the path is correct.

If Note entity has isPrivate column defined and I'm trying to use a find method from repository instance, a following error gets thrown:

no such column: Note.isPrivate (code 1 SQLITE_ERROR): , while compiling: SELECT &quot;Note&quot;.&quot;id&quot; AS &quot;Note_id&quot;, &quot;Note&quot;.&quot;title&quot; AS &quot;Note_title&quot;, &quot;Note&quot;.&quot;details&quot; AS &quot;Note_details&quot;, &quot;Note&quot;.&quot;isPrivate&quot; AS &quot;Note_isPrivate&quot;, &quot;Note&quot;.&quot;folderId&quot; AS &quot;Note_folderId&quot; FROM &quot;note&quot; &quot;Note&quot;

Here is data-source.ts file:

import &quot;reflect-metadata&quot;;
import { DataSource } from &quot;typeorm&quot;;
import * as sqlitedriver from &#39;expo-sqlite&#39;;
import Folder from &#39;./entities/Folder.model.ts&#39;;
import Note from &#39;./entities/Note.model.ts&#39;;

const AppDataSource = new DataSource({
    driver: sqlitedriver,
    database: &#39;app_db&#39;,
    type: &#39;expo&#39;,
    migrationsRun: true,
    entities: [Folder, Note],
    migrations: [&#39;migrations/*.ts&#39;],
});

AppDataSource.initialize().then(() =&gt; {
    console.log(&quot;Data Source has been initialized!&quot;);
}).catch((err) =&gt; {
    console.error(&quot;Error during Data Source initialization&quot;, err.message);
});

export default AppDataSource;

migration file:

import { MigrationInterface, QueryRunner } from &quot;typeorm&quot;;
import { TableColumn } from &quot;typeorm&quot;;

export class AddIsPrivateColumn1684782594104 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise&lt;void&gt; {
        await queryRunner.addColumn(
            &quot;Note&quot;,
            new TableColumn({
                name: &quot;isPrivate&quot;,
                type: &quot;boolean&quot;,
            }),
        );
    };

    public async down(queryRunner: QueryRunner): Promise&lt;void&gt; {
        await queryRunner.dropColumn(&quot;Note&quot;, &quot;isPrivate&quot;);
    };
};

How do I run a migration? Is something wrong with the data source configuration?

答案1

得分: 0

好的,这是翻译好的部分:

我的问题出在迁移在数据源配置对象中的列举方式上。由于某种原因,我无法将它们指定为路径字符串,因此我直接导入了迁移类,并将它们添加到 migrations 数组中。

英文:

Ok, so I think I solved it. My problem was in a way migrations were listed in a data source configuration object. For some reason I couldn't specify them as a path string, so instead I imported migrations classes directly and added them to array of migrations.

huangapple
  • 本文由 发表于 2023年6月1日 02:28:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定