TypeORM—使用自定义模式运行迁移



我需要运行创建一堆表的TypeORM迁移。已创建MS-SQL数据库。这些表是模式domain1的一部分。下面是ORM配置和迁移。

配置

return {
type: 'mssql',
host: '..',
port: '..',
username: '..',
password: '..',
database: '..',
options: {
encrypt: true,
},
entities: [`${__dirname}/src/entities/*.entity{.ts,.js}`],
migrations: [`${__dirname}/migrations/**/*{.ts,.js}`],
migrationsRun: true,
synchronize: false,
logging: true,
schema: 'domain1',
cli: {
migrationsDir: 'apps/myapp/src/migration',
},
};

迁移:

export class addBasicSchema1411753217156 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
console.log('inside up method');
await queryRunner.createSchema('domain1', true);
await queryRunner.query(
`IF NOT EXISTS (SELECT * FROM sys.tables t JOIN sys.schemas s ON (t.schema_id = s.schema_id) WHERE s.name = 'domain1' AND t.name = 'table1') CREATE TABLE "domain1".[table1] (..);`,
);
await queryRunner.query(
`IF NOT EXISTS (SELECT * FROM sys.tables t JOIN sys.schemas s ON (t.schema_id = s.schema_id) WHERE s.name = 'domain1' AND t.name = 'table2') CREATE TABLE "domain1".[table2] (..);`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "domain1"."table1"`);
await queryRunner.query(`DROP TABLE "domain1"."table2"`);
await queryRunner.dropSchema('domain1', true);
}
}

这里的问题是,在模式创建之前,它试图用domain1.migration创建迁移表. 流量从未达到console.log('inside up method');

CREATE TABLE "mydb"."domain1"."migrations" ("id" int NOT NULL IDENTITY(1,1), "timestamp" bigint NOT NULL, 
"name" varchar(255) NOT NULL, CONSTRAINT "PK_8b82d7f526340ab734260ea46b1" PRIMARY KEY ("id"))

并抛出错误:QueryFailedError: Error: The specified schema name "domain1" either does not exist or you do not have permission to use it.

您可能没有创建新模式(或数据库)的权限。

我遇到了一个类似的问题,我通过手动创建数据库来解决它。

我的情况的根本原因是我在typeorm中配置的用户没有创建模式的权限。.

错误实际上说明了问题。模式/数据库domain1不存在,或者您通过的用户没有权限使用它

请检查您已经在MySQL中创建了数据库,您可以尝试使用MySQL Workbench作为GUI应用程序。如果没有,那就创建一个

还有一个关于您用来连接到数据库的用户的问题。您可以尝试创建一个新用户,并授予他对模式的访问权限,但很可能没有创建模式。

我知道这个问题有点老了,但我和你一样面临着同样的问题,几乎放弃了这个项目。

所以,对我来说,问题是因为我在DataSource配置上设置了模式的名称,当类型运行迁移时,他试图与DataSource配置上设置的默认模式连接,由于它不存在,连接失败,当我将模式名称从DataSource更改为公共时,因为它已经存在,迁移成功运行。

我希望这个答案能帮到你。

最新更新