Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6442 fix: @JoinTable does not respect inverseJoinColumns referenced column width #6444

Merged
merged 4 commits into from Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}));
});
});