diff --git a/src/entity-schema/EntitySchemaRelationOptions.ts b/src/entity-schema/EntitySchemaRelationOptions.ts index 953caaccd6..0502fc67e6 100644 --- a/src/entity-schema/EntitySchemaRelationOptions.ts +++ b/src/entity-schema/EntitySchemaRelationOptions.ts @@ -55,7 +55,7 @@ export interface EntitySchemaRelationOptions { /** * Join column options of this column. If set to true then it simply means that it has a join column. */ - joinColumn?: boolean|JoinColumnOptions; + joinColumn?: boolean|JoinColumnOptions|JoinColumnOptions[]; /** * Indicates if this is a parent (can be only many-to-one relation) relation in the tree tables. diff --git a/src/entity-schema/EntitySchemaTransformer.ts b/src/entity-schema/EntitySchemaTransformer.ts index 8180881613..e53eb2c981 100644 --- a/src/entity-schema/EntitySchemaTransformer.ts +++ b/src/entity-schema/EntitySchemaTransformer.ts @@ -151,13 +151,17 @@ export class EntitySchemaTransformer { }; metadataArgsStorage.joinColumns.push(joinColumn); } else { - const joinColumn: JoinColumnMetadataArgs = { - target: options.target || options.name, - propertyName: relationName, - name: relationSchema.joinColumn.name, - referencedColumnName: relationSchema.joinColumn.referencedColumnName - }; - metadataArgsStorage.joinColumns.push(joinColumn); + const joinColumnsOptions = Array.isArray(relationSchema.joinColumn) ? relationSchema.joinColumn : [relationSchema.joinColumn]; + + for (const joinColumnOption of joinColumnsOptions) { + const joinColumn: JoinColumnMetadataArgs = { + target: options.target || options.name, + propertyName: relationName, + name: joinColumnOption.name, + referencedColumnName: joinColumnOption.referencedColumnName + }; + metadataArgsStorage.joinColumns.push(joinColumn); + } } } diff --git a/test/github-issues/5444/entity/Author.ts b/test/github-issues/5444/entity/Author.ts new file mode 100644 index 0000000000..20191b21c5 --- /dev/null +++ b/test/github-issues/5444/entity/Author.ts @@ -0,0 +1,41 @@ +import {EntitySchemaOptions} from "../../../../src/entity-schema/EntitySchemaOptions"; +import {Post} from "./Post"; + +export class Author { + id: number; + + publisherId: number; + + name: string; + + posts: Post[]; +} + +export const AuthorSchema: EntitySchemaOptions = { + name: "Author", + + target: Author, + + columns: { + id: { + primary: true, + type: Number + }, + + publisherId: { + primary: true, + type: Number + }, + + name: { + type: "varchar" + } + }, + + relations: { + posts: { + target: () => Post, + type: "one-to-many" + } + } +}; diff --git a/test/github-issues/5444/entity/Post.ts b/test/github-issues/5444/entity/Post.ts new file mode 100644 index 0000000000..eb5239960c --- /dev/null +++ b/test/github-issues/5444/entity/Post.ts @@ -0,0 +1,53 @@ +import {EntitySchemaOptions} from "../../../../src/entity-schema/EntitySchemaOptions"; +import {Author} from "./Author"; + +export class Post { + authorPublisherId: number; + + authorId: number; + + id: number; + + title: string; + + author: Author; +} + +export const PostSchema: EntitySchemaOptions = { + name: "Post", + + target: Post, + + columns: { + authorPublisherId: { + primary: true, + type: Number + }, + + authorId: { + primary: true, + type: Number + }, + + id: { + primary: true, + type: Number + }, + + title: { + type: "varchar" + } + }, + + relations: { + author: { + target: () => Author, + type: "many-to-one", + eager: true, + joinColumn: [ + { name: "authorPublisherId", referencedColumnName: "publisherId" }, + { name: "authorId", referencedColumnName: "id" }, + ] + } + } +}; diff --git a/test/github-issues/5444/issue-5444.ts b/test/github-issues/5444/issue-5444.ts new file mode 100644 index 0000000000..b25f47cd8d --- /dev/null +++ b/test/github-issues/5444/issue-5444.ts @@ -0,0 +1,40 @@ +import { EntitySchemaTransformer } from "../../../src/entity-schema/EntitySchemaTransformer"; + +import {expect} from "chai"; + +import { Post, PostSchema } from "./entity/Post"; +import { Author, AuthorSchema } from "./entity/Author"; +import {EntitySchema} from "../../../src"; + + +describe("github issues > #5444 EntitySchema missing support for multiple joinColumns in relations", () => { + it("Update query returns the number of affected rows", async () => { + const transformer = new EntitySchemaTransformer(); + + const actual = transformer.transform( + [ + new EntitySchema(AuthorSchema), + new EntitySchema(PostSchema) + ] + ); + + const joinColumns = actual.joinColumns; + + expect(joinColumns.length).to.eq(2); + expect(joinColumns).to.deep.eq([ + { + target: Post, + propertyName: "author", + name: "authorPublisherId", + referencedColumnName: "publisherId" + }, + { + target: Post, + propertyName: "author", + name: "authorId", + referencedColumnName: "id" + }, + + ]); + }); +});