我需要运行创建一堆表的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.
发布于 2021-11-08 12:51:15
您可能没有创建新架构(或数据库)的权限。
我面临着一个类似的问题,我通过手动创建数据库来解决这个问题。
我的情况的根本原因是我在typeorm 中配置的用户没有创建模式的权限。
发布于 2021-10-27 09:53:24
这个错误实际上说明了这个问题。模式/数据库domain1不存在,或者您传递的用户没有使用它的权限。
请检查您是否在MySQL中创建了数据库,您可以尝试使用MySQL工作台作为GUI应用程序。如果它不在那里,那就创建它。
此外,还有一个关于您要连接到数据库的用户的问题。您可以尝试创建一个新用户并授予他对架构的访问权限,但很可能没有创建架构。
https://stackoverflow.com/questions/69734932
复制相似问题