fix: loading tables with fk in sqlite query runner (#9875)

Closes: #9266
This commit is contained in:
GP4cK 2023-04-06 14:31:32 +08:00 committed by GitHub
parent 0619aca174
commit 4997da054b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 1 deletions

View File

@ -1570,7 +1570,7 @@ export abstract class AbstractSqliteQueryRunner
)
return new TableForeignKey({
name: fkMapping!.name,
name: fkMapping?.name,
columnNames: columnNames,
referencedTableName: foreignKey["table"],
referencedColumnNames: referencedColumnNames,

View File

@ -0,0 +1,33 @@
import { expect } from "chai"
import "reflect-metadata"
import { DataSource } from "../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
describe("github issues > #9266 queryRunner.getTable() fails if Foreign Key is set in target table", () => {
let connections: DataSource[]
before(async () => {
connections = await createTestingConnections({
migrations: [__dirname + "/migrations/*{.js,.ts}"],
enabledDrivers: ["sqlite", "better-sqlite3"],
})
})
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("should be able to load tables", () =>
Promise.all(
connections.map(async (connection) => {
await connection.runMigrations()
const queryRunner = connection.createQueryRunner()
const tables = await queryRunner.getTables()
const tableNames = tables.map((table) => table.name)
expect(tableNames).to.include("author")
expect(tableNames).to.include("post")
await queryRunner.release()
}),
))
})

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "../../../../src"
export class CreateDatabase implements MigrationInterface {
name = "CreateDatabase1623518107000"
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "author" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL)`,
)
await queryRunner.query(
`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "text" varchar NOT NULL, "authorId" integer NOT NULL REFERENCES author(id) ON DELETE CASCADE ON UPDATE NO ACTION)`,
)
}
async down(queryRunner: QueryRunner): Promise<void> {}
}