mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed bug with custom join column name
This commit is contained in:
parent
2e9febf212
commit
76313703a0
@ -43,7 +43,10 @@ createConnection(options).then(connection => {
|
||||
.persist(post)
|
||||
.then(post => {
|
||||
console.log("Post has been saved. Lets load it now.");
|
||||
return postRepository.find({ alias: "post", leftJoinAndSelect: { categories: "post.categories"} });
|
||||
return postRepository.find({ alias: "post", leftJoinAndSelect: {
|
||||
categories: "post.categories",
|
||||
author: "post.user" // note that table column is used, not object property
|
||||
}});
|
||||
})
|
||||
.then(loadedPosts => {
|
||||
console.log("loadedPosts: ", loadedPosts);
|
||||
|
||||
@ -22,9 +22,9 @@ export class Post {
|
||||
@ManyToOne(type => Author, author => author.posts, {
|
||||
cascadeAll: true
|
||||
})
|
||||
@JoinColumn(/*{ // todo: not yet fixed
|
||||
@JoinColumn({ // todo: not yet fixed
|
||||
name: "user"
|
||||
}*/)
|
||||
})
|
||||
author: Author;
|
||||
|
||||
@ManyToMany(type => Category, category => category.posts, {
|
||||
|
||||
@ -70,8 +70,8 @@ export class EntityMetadataBuilder {
|
||||
const tableMetadatas = allTableMetadatas.filterByClasses(entityClasses).filter(table => !table.isAbstract);
|
||||
|
||||
// set naming strategy
|
||||
allMetadataStorage.tableMetadatas.forEach(tableMetadata => tableMetadata.namingStrategy = this.namingStrategy);
|
||||
allTableMetadatas.forEach(column => column.namingStrategy = this.namingStrategy);
|
||||
// allMetadataStorage.tableMetadatas.forEach(tableMetadata => tableMetadata.namingStrategy = this.namingStrategy);
|
||||
// allTableMetadatas.forEach(column => column.namingStrategy = this.namingStrategy);
|
||||
// entityMetadata.relations.forEach(relation => relation.namingStrategy = this.namingStrategy);
|
||||
|
||||
const entityMetadatas = tableMetadatas.map(tableMetadata => {
|
||||
@ -79,7 +79,7 @@ export class EntityMetadataBuilder {
|
||||
const mergedMetadata = allMetadataStorage.mergeWithAbstract(allTableMetadatas, tableMetadata);
|
||||
|
||||
// set naming strategy
|
||||
tableMetadata.namingStrategy = this.namingStrategy;
|
||||
// tableMetadata.namingStrategy = this.namingStrategy;
|
||||
mergedMetadata.columnMetadatas.forEach(column => column.namingStrategy = this.namingStrategy);
|
||||
mergedMetadata.relationMetadatas.forEach(relation => relation.namingStrategy = this.namingStrategy);
|
||||
mergedMetadata.indexMetadatas.forEach(relation => relation.namingStrategy = this.namingStrategy);
|
||||
@ -90,12 +90,12 @@ export class EntityMetadataBuilder {
|
||||
|
||||
// create a new entity metadata
|
||||
const entityMetadata = new EntityMetadata(
|
||||
this.namingStrategy,
|
||||
tableMetadata,
|
||||
mergedMetadata.columnMetadatas,
|
||||
mergedMetadata.relationMetadatas,
|
||||
mergedMetadata.compositeIndexMetadatas
|
||||
);
|
||||
entityMetadata.namingStrategy = this.namingStrategy;
|
||||
|
||||
// create entity's relations join tables
|
||||
entityMetadata.manyToManyRelations.forEach(relation => {
|
||||
@ -149,7 +149,6 @@ export class EntityMetadataBuilder {
|
||||
let relationalColumn = metadata.columns.find(column => column.name === relation.name); // todo?: ColumnCollection.findByName
|
||||
if (!relationalColumn) {
|
||||
const options: ColumnOptions = {
|
||||
name: relation.joinColumn.name,
|
||||
type: inverseSideColumn.type,
|
||||
oldColumnName: relation.oldColumnName,
|
||||
nullable: relation.isNullable
|
||||
@ -180,16 +179,6 @@ export class EntityMetadataBuilder {
|
||||
const junctionEntityMetadatas: EntityMetadata[] = [];
|
||||
entityMetadatas.forEach(metadata => {
|
||||
metadata.ownerManyToManyRelations.map(relation => {
|
||||
const inverseSideMetadata = relation.inverseEntityMetadata;
|
||||
// const inverseSideColumn = relation.inverseSideColumn;
|
||||
|
||||
// generate table name for junction table
|
||||
/*const tableName = this.namingStrategy.joinTableName(
|
||||
metadata.table.name,
|
||||
inverseSideMetadata.table.name,
|
||||
relation.name,
|
||||
inverseSideColumn.name
|
||||
);*/
|
||||
const tableMetadata = new JunctionTableMetadata(relation.joinTable.name);
|
||||
const column1 = relation.joinTable.referencedColumn;
|
||||
const column2 = relation.joinTable.inverseReferencedColumn;
|
||||
@ -214,10 +203,10 @@ export class EntityMetadataBuilder {
|
||||
options: column2options
|
||||
})
|
||||
];
|
||||
const junctionEntityMetadata = new EntityMetadata(tableMetadata, columns, [], []);
|
||||
const junctionEntityMetadata = new EntityMetadata(this.namingStrategy, tableMetadata, columns, [], []);
|
||||
junctionEntityMetadata.foreignKeys.push(
|
||||
new ForeignKeyMetadata(tableMetadata, [columns[0]], metadata.table, [column1]),
|
||||
new ForeignKeyMetadata(tableMetadata, [columns[1]], inverseSideMetadata.table, [column2])
|
||||
new ForeignKeyMetadata(tableMetadata, [columns[1]], relation.inverseEntityMetadata.table, [column2])
|
||||
);
|
||||
junctionEntityMetadatas.push(junctionEntityMetadata);
|
||||
relation.junctionEntityMetadata = junctionEntityMetadata;
|
||||
|
||||
@ -11,18 +11,14 @@ import {NamingStrategyInterface} from "../naming-strategy/NamingStrategy";
|
||||
*/
|
||||
export class EntityMetadata {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Public Properties
|
||||
// ---------------------------------------------------------------------
|
||||
// -------------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Naming strategy used to generate and normalize column name.
|
||||
*/
|
||||
namingStrategy: NamingStrategyInterface;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
// -------------------------------------------------------------------------
|
||||
readonly namingStrategy: NamingStrategyInterface;
|
||||
|
||||
readonly table: TableMetadata;
|
||||
readonly columns: ColumnMetadata[];
|
||||
@ -34,15 +30,18 @@ export class EntityMetadata {
|
||||
// Constructor
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(table: TableMetadata,
|
||||
columns: ColumnMetadata[],
|
||||
relations: RelationMetadata[],
|
||||
compositeIndices: CompositeIndexMetadata[]) {
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.relations = relations;
|
||||
this.compositeIndices = compositeIndices;
|
||||
|
||||
constructor(namingStrategy: NamingStrategyInterface,
|
||||
tableMetadata: TableMetadata,
|
||||
columnMetadatas: ColumnMetadata[],
|
||||
relationMetadatas: RelationMetadata[],
|
||||
compositeIndexMetadatas: CompositeIndexMetadata[]) {
|
||||
this.namingStrategy = namingStrategy;
|
||||
this.table = tableMetadata;
|
||||
this.columns = columnMetadatas;
|
||||
this.relations = relationMetadatas;
|
||||
this.compositeIndices = compositeIndexMetadatas;
|
||||
|
||||
this.table.entityMetadata = this;
|
||||
this.relations.forEach(relation => relation.entityMetadata = this);
|
||||
this.compositeIndices.forEach(index => index.entityMetadata = this);
|
||||
}
|
||||
|
||||
@ -158,6 +158,9 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
get name(): string {
|
||||
if (this.joinColumn && this.joinColumn.name)
|
||||
return this.joinColumn.name;
|
||||
|
||||
return this.namingStrategy ? this.namingStrategy.relationName(this._name) : this._name;
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import {NamingStrategyInterface} from "../naming-strategy/NamingStrategy";
|
||||
import {TargetMetadata} from "./TargetMetadata";
|
||||
import {EntityMetadata} from "./EntityMetadata";
|
||||
|
||||
/**
|
||||
* This metadata interface contains all information about specific table.
|
||||
@ -13,7 +13,7 @@ export class TableMetadata extends TargetMetadata {
|
||||
/**
|
||||
* Naming strategy used to generate and normalize table name.
|
||||
*/
|
||||
namingStrategy: NamingStrategyInterface;
|
||||
entityMetadata: EntityMetadata;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
@ -60,7 +60,7 @@ export class TableMetadata extends TargetMetadata {
|
||||
if (this._name)
|
||||
return this._name;
|
||||
|
||||
return this.namingStrategy ? this.namingStrategy.tableName((<any>this.target).name) : (<any>this.target).name;
|
||||
return this.entityMetadata.namingStrategy.tableName((<any>this.target).name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user