Skip to content

Instantly share code, notes, and snippets.

@michaelbromley
Created May 8, 2020 08:13
Show Gist options
  • Save michaelbromley/99eecfe6d7efbc2fbb37f7cbec7b84b2 to your computer and use it in GitHub Desktop.
Save michaelbromley/99eecfe6d7efbc2fbb37f7cbec7b84b2 to your computer and use it in GitHub Desktop.
TypeORM failing schema sync test
import { Entity, PrimaryColumn } from "../../../../src";
@Entity()
export class Asset {
@PrimaryColumn()
id: number;
}
import "reflect-metadata";
import { expect } from "chai";
import { Connection } from "../../../src";
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Product } from "./entity/Product";
import { Asset } from "./entity/Asset";
describe.only("schema sync", () => {
let connections: Connection[];
before(async () => {
connections = await createTestingConnections({
entities: [Product, Asset],
schemaCreate: true,
dropSchema: true
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should not drop and re-add foreign key", () =>
Promise.all(connections.map(async (connection) => {
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
// FAILS (TypeORM 0.2.24) - there will be 2 up queries:
// - "ALTER TABLE `product` DROP FOREIGN KEY `FK_91a19e6613534949a4ce6e76ff8`"
// - "ALTER TABLE `product` ADD CONSTRAINT `FK_91a19e6613534949a4ce6e76ff8` FOREIGN KEY (`featuredAssetId`) REFERENCES `asset`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION"
expect(sqlInMemory.upQueries.length).to.eq(0);
})));
});
import { Entity, ManyToOne, PrimaryColumn } from "../../../../src";
import { Asset } from "./Asset";
@Entity()
export class Product {
@PrimaryColumn()
id: number;
@ManyToOne((type) => Asset, {onDelete: "SET NULL", nullable: true})
featuredAsset: Asset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment