英文:
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 "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"
Here is data-source.ts file:
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("Data Source has been initialized!");
}).catch((err) => {
console.error("Error during Data Source initialization", err.message);
});
export default AppDataSource;
migration file:
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");
};
};
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论