diff --git a/src/metadata-builder/JunctionEntityMetadataBuilder.ts b/src/metadata-builder/JunctionEntityMetadataBuilder.ts index e546432907..9fbb1ed733 100644 --- a/src/metadata-builder/JunctionEntityMetadataBuilder.ts +++ b/src/metadata-builder/JunctionEntityMetadataBuilder.ts @@ -113,6 +113,7 @@ export class JunctionEntityMetadataBuilder { && (inverseReferencedColumn.generationStrategy === "uuid" || inverseReferencedColumn.type === "uuid") ? "36" : inverseReferencedColumn.length, // fix https://github.com/typeorm/typeorm/issues/3604 + width: inverseReferencedColumn.width, // fix https://github.com/typeorm/typeorm/issues/6442 type: inverseReferencedColumn.type, precision: inverseReferencedColumn.precision, scale: inverseReferencedColumn.scale, diff --git a/test/github-issues/6642/entity/v1/entities.ts b/test/github-issues/6642/entity/v1/entities.ts new file mode 100644 index 0000000000..f8df17ccd2 --- /dev/null +++ b/test/github-issues/6642/entity/v1/entities.ts @@ -0,0 +1,18 @@ +import { PrimaryColumn, Generated, Column, Entity } from "../../../../../src"; + +@Entity() +export class FooEntity { + @PrimaryColumn({ type: "int", width: 10, unsigned: true, nullable: false }) + @Generated() + public id: number; +} + +@Entity() +export class BarEntity { + @PrimaryColumn({ type: "int", width: 10, unsigned: true }) + @Generated() + public id: number; + + @Column("varchar", { nullable: false, length: 50 }) + public code: string; +} diff --git a/test/github-issues/6642/entity/v2/entities.ts b/test/github-issues/6642/entity/v2/entities.ts new file mode 100644 index 0000000000..fcf2d1a14b --- /dev/null +++ b/test/github-issues/6642/entity/v2/entities.ts @@ -0,0 +1,34 @@ +import { PrimaryColumn, Generated, ManyToMany, Entity, Column, JoinTable } from "../../../../../src"; + +@Entity() +export class FooEntity { + @PrimaryColumn({ type: "int", width: 10, unsigned: true, nullable: false }) + @Generated() + public id: number; + + @ManyToMany(() => BarEntity) + @JoinTable({ + name: "foo_bars", + joinColumns: [ + { + name: "foo_id", + } + ], + inverseJoinColumns: [ + { + name: "bar_id", + } + ] + }) + public fooBars: BarEntity[]; +} + +@Entity() +export class BarEntity { + @PrimaryColumn({ type: "int", width: 10, unsigned: true }) + @Generated() + public id: number; + + @Column("varchar", { nullable: false, length: 50 }) + public code: string; +} diff --git a/test/github-issues/6642/issue-6642.ts b/test/github-issues/6642/issue-6642.ts new file mode 100644 index 0000000000..b8ba4f7d05 --- /dev/null +++ b/test/github-issues/6642/issue-6642.ts @@ -0,0 +1,67 @@ +import "reflect-metadata"; +import { + closeTestingConnections, + createTestingConnections, + reloadTestingDatabases, + setupSingleTestingConnection +} from "../../utils/test-utils"; +import { Connection, createConnection } from "../../../src"; +import { fail } from "assert"; +import { Query } from "../../../src/driver/Query"; +import { MysqlConnectionOptions } from "../../../src/driver/mysql/MysqlConnectionOptions"; + +describe("github issues > #6642 JoinTable does not respect inverseJoinColumns referenced column width", () => { + let connections: Connection[]; + + before(async () => { + return connections = await createTestingConnections({ + entities: [__dirname + "/entity/v1/*{.js,.ts}"], + schemaCreate: true, + dropSchema: true, + enabledDrivers: ["mysql"] + }); + }); + beforeEach(async () => await reloadTestingDatabases(connections)); + after(async () => await closeTestingConnections(connections)); + + it("should generate column widths equal to the referenced column widths", async () => { + + await Promise.all(connections.map(async (connection) => { + const options = setupSingleTestingConnection( + connection.options.type, + { + name: `${connection.name}-v2`, + entities: [__dirname + "/entity/v2/*{.js,.ts}"], + dropSchema: false, + schemaCreate: false + } + ) as MysqlConnectionOptions; + + if (!options) { + await connection.close(); + fail(); + } + + const migrationConnection = await createConnection(options); + try { + const sqlInMemory = await migrationConnection.driver + .createSchemaBuilder() + .log(); + + const upQueries = sqlInMemory.upQueries.map( + (query: Query) => query.query + ); + + upQueries.should.eql([ + "CREATE TABLE `foo_bars` (`foo_id` int(10) UNSIGNED NOT NULL, `bar_id` int(10) UNSIGNED NOT NULL, INDEX `IDX_319290776f044043e3ef3ba5a8` (`foo_id`), INDEX `IDX_b7fd4be386fa7cdb87ef8b12b6` (`bar_id`), PRIMARY KEY (`foo_id`, `bar_id`)) ENGINE=InnoDB", + "ALTER TABLE `foo_bars` ADD CONSTRAINT `FK_319290776f044043e3ef3ba5a8d` FOREIGN KEY (`foo_id`) REFERENCES `foo_entity`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION", + "ALTER TABLE `foo_bars` ADD CONSTRAINT `FK_b7fd4be386fa7cdb87ef8b12b69` FOREIGN KEY (`bar_id`) REFERENCES `bar_entity`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION" + ]); + + } finally { + await connection.close(); + await migrationConnection.close(); + } + })); + }); +});