Skip to content

Commit

Permalink
fix: @jointable does not respect inverseJoinColumns referenced column…
Browse files Browse the repository at this point in the history
… width (#6444)

* fix: provide width to ColumnMetadataArgs for JoinTable

@jointable does not respect inverseJoinColumns referenced column width

Closes: #6442

* fix: address linting errors, replaced single quotes

* fix: add await so that promises are resolved

* fix: add connection close for migration connection
  • Loading branch information
michael-golden committed Jul 24, 2020
1 parent 259ad0e commit f642a9e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/metadata-builder/JunctionEntityMetadataBuilder.ts
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions 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;
}
34 changes: 34 additions & 0 deletions 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;
}
67 changes: 67 additions & 0 deletions 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();
}
}));
});
});

0 comments on commit f642a9e

Please sign in to comment.