Skip to content

Commit

Permalink
fix(core): fix explicit usage of joinColumn option
Browse files Browse the repository at this point in the history
Closes #425
  • Loading branch information
Martin Adamek committed Mar 26, 2020
1 parent 2d2c73d commit bcf2546
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
42 changes: 15 additions & 27 deletions lib/metadata/MetadataDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,55 +241,43 @@ export class MetadataDiscovery {
prop.fixedOrderColumn = prop2.fixedOrderColumn;
}

if (!prop.referencedColumnNames || prop.referencedColumnNames.length === 0) {
if (!prop.referencedColumnNames) {
prop.referencedColumnNames = Utils.flatten(meta.primaryKeys.map(primaryKey => meta.properties[primaryKey].fieldNames));
}

if (!prop.joinColumns || prop.joinColumns.length === 0) {
if (!prop.joinColumns) {
prop.joinColumns = prop.referencedColumnNames.map(referencedColumnName => this.namingStrategy.joinKeyColumnName(meta.collection, referencedColumnName));
}

if (!prop.inverseJoinColumns || prop.inverseJoinColumns.length === 0) {
if (!prop.inverseJoinColumns) {
const meta2 = this.metadata.get(prop.type);
prop.inverseJoinColumns = this.initManyToOneFieldName(prop, meta2.collection);
}
}

private initManyToOneFields(prop: EntityProperty): void {
if (prop.joinColumns) {
return;
}

const meta2 = this.metadata.get(prop.type);
prop.joinColumns = [];
prop.referencedColumnNames = [];
prop.referencedTableName = meta2.collection;
const fieldNames = Utils.flatten(meta2.primaryKeys.map(primaryKey => meta2.properties[primaryKey].fieldNames));
Utils.defaultValue(prop, 'referencedTableName', meta2.collection);

for (const primaryKey of meta2.primaryKeys) {
const prop2 = meta2.properties[primaryKey];
if (!prop.joinColumns) {
prop.joinColumns = fieldNames.map(fieldName => this.namingStrategy.joinKeyColumnName(prop.name, fieldName));
}

for (const fieldName of prop2.fieldNames) {
prop.joinColumns.push(this.namingStrategy.joinKeyColumnName(prop.name, fieldName));
prop.referencedColumnNames.push(fieldName);
}
if (!prop.referencedColumnNames) {
prop.referencedColumnNames = fieldNames;
}
}

private initOneToManyFields(prop: EntityProperty): void {
if (prop.joinColumns) {
return;
}

const meta2 = this.metadata.get(prop.type);
prop.joinColumns = [this.namingStrategy.joinColumnName(prop.name)];
prop.referencedColumnNames = [];

for (const primaryKey of meta2.primaryKeys) {
const prop2 = meta2.properties[primaryKey];
if (!prop.joinColumns) {
prop.joinColumns = [this.namingStrategy.joinColumnName(prop.name)];
}

for (const fieldName of prop2.fieldNames) {
prop.referencedColumnNames.push(fieldName);
}
if (!prop.referencedColumnNames) {
prop.referencedColumnNames = Utils.flatten(meta2.primaryKeys.map(primaryKey => meta2.properties[primaryKey].fieldNames));
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/schema/EntitySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class EntitySchema<T extends AnyEntity<T> = AnyEntity, U extends AnyEntit
const rename = <U> (data: U, from: string, to: string): void => {
if (options[from] && !options[to]) {
options[to] = [options[from]];
delete options[from];
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/entities-sql/Address2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Author2 } from './Author2';
@Entity()
export class Address2 {

@OneToOne({ entity: () => Author2, primary: true })
@OneToOne({ entity: () => Author2, primary: true, joinColumn: 'author_id' })
author: Author2;

@Property()
Expand Down

0 comments on commit bcf2546

Please sign in to comment.