Skip to content

Commit

Permalink
feat: add constraintName to JoinColumn
Browse files Browse the repository at this point in the history
Add a constraintName to JoinColumn decorators to allow specifying foreignKey name.
Use constraintName when building JoinTable entities as well.

Partially solves: typeorm#1355
  • Loading branch information
M-TGH committed Jan 17, 2022
1 parent 26581d0 commit 6f623c2
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/decorator/options/JoinColumnOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ export interface JoinColumnOptions {
*/
referencedColumnName?: string; // TODO rename to referencedColumn

/**
* Name of the foreign key constraint.
*/
constraintName?: string;

}
3 changes: 2 additions & 1 deletion src/decorator/relations/JoinColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function JoinColumn(optionsOrOptionsArray?: JoinColumnOptions|JoinColumnO
target: object.constructor,
propertyName: propertyName,
name: options.name,
referencedColumnName: options.referencedColumnName
referencedColumnName: options.referencedColumnName,
constraintName: options.constraintName,
} as JoinColumnMetadataArgs);
});
};
Expand Down
3 changes: 2 additions & 1 deletion src/entity-schema/EntitySchemaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ export class EntitySchemaTransformer {
target: options.target || options.name,
propertyName: relationName,
name: joinColumnOption.name,
referencedColumnName: joinColumnOption.referencedColumnName
referencedColumnName: joinColumnOption.referencedColumnName,
constraintName: joinColumnOption.constraintName,
};
metadataArgsStorage.joinColumns.push(joinColumn);
}
Expand Down
5 changes: 5 additions & 0 deletions src/metadata-args/JoinColumnMetadataArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ export interface JoinColumnMetadataArgs {
*/
referencedColumnName?: string;

/**
* Name of the foreign key constraint.
*/
constraintName?: string;

}
4 changes: 4 additions & 0 deletions src/metadata-builder/JunctionEntityMetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class JunctionEntityMetadataBuilder {
connection: this.connection,
entityMetadata: entityMetadata,
referencedColumn: referencedColumn,
constraintName: joinColumn?.constraintName,
args: {
target: "",
mode: "virtual",
Expand Down Expand Up @@ -107,6 +108,7 @@ export class JunctionEntityMetadataBuilder {
connection: this.connection,
entityMetadata: entityMetadata,
referencedColumn: inverseReferencedColumn,
constraintName: joinColumn?.constraintName,
args: {
target: "",
mode: "virtual",
Expand Down Expand Up @@ -147,6 +149,7 @@ export class JunctionEntityMetadataBuilder {
// Note: UPDATE CASCADE clause is not supported in Oracle.
entityMetadata.foreignKeys = relation.createForeignKeyConstraints ? [
new ForeignKeyMetadata({
name: junctionColumns[0]?.constraintName,
entityMetadata: entityMetadata,
referencedEntityMetadata: relation.entityMetadata,
columns: junctionColumns,
Expand All @@ -155,6 +158,7 @@ export class JunctionEntityMetadataBuilder {
onUpdate: this.connection.driver instanceof OracleDriver ? "NO ACTION" : relation.onUpdate || "CASCADE",
}),
new ForeignKeyMetadata({
name: inverseJunctionColumns[0]?.constraintName,
entityMetadata: entityMetadata,
referencedEntityMetadata: relation.inverseEntityMetadata,
columns: inverseJunctionColumns,
Expand Down
1 change: 1 addition & 0 deletions src/metadata-builder/RelationJoinColumnBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class RelationJoinColumnBuilder {
return { foreignKey: undefined, columns, uniqueConstraint: undefined }; // this case is possible for one-to-one non owning side and relations with createForeignKeyConstraints = false

const foreignKey = new ForeignKeyMetadata({
name: joinColumns[0]?.constraintName,
entityMetadata: relation.entityMetadata,
referencedEntityMetadata: relation.inverseEntityMetadata,
namingStrategy: this.connection.namingStrategy,
Expand Down
7 changes: 7 additions & 0 deletions src/metadata/ColumnMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ export class ColumnMetadata {
*/
referencedColumn: ColumnMetadata|undefined;

/**
* If this column is foreign key then this specifies the name for it.
*/
constraintName?: string;

/**
* Specifies a value transformer that is to be used to (un)marshal
* this column when reading or writing to the database.
Expand Down Expand Up @@ -320,6 +325,7 @@ export class ColumnMetadata {
entityMetadata: EntityMetadata,
embeddedMetadata?: EmbeddedMetadata,
referencedColumn?: ColumnMetadata,
constraintName?: string,
args: ColumnMetadataArgs,
closureType?: "ancestor"|"descendant",
nestedSetLeft?: boolean,
Expand All @@ -329,6 +335,7 @@ export class ColumnMetadata {
this.entityMetadata = options.entityMetadata;
this.embeddedMetadata = options.embeddedMetadata!;
this.referencedColumn = options.referencedColumn;
this.constraintName = options.constraintName;
if (options.args.target)
this.target = options.args.target;
if (options.args.propertyName)
Expand Down
11 changes: 10 additions & 1 deletion src/metadata/ForeignKeyMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export class ForeignKeyMetadata {

/**
* Gets foreign key name.
* If unique constraint name was given by a user then it stores givenName.
* If unique constraint name was not given then its generated.
*/
name: string;

Expand All @@ -69,6 +71,11 @@ export class ForeignKeyMetadata {
*/
referencedColumnNames: string[] = [];

/**
* User specified unique constraint name.
*/
givenName?: string;

// ---------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------
Expand All @@ -82,6 +89,7 @@ export class ForeignKeyMetadata {
onDelete?: OnDeleteType,
onUpdate?: OnUpdateType,
deferrable?: DeferrableType,
name?: string,
}) {
this.entityMetadata = options.entityMetadata;
this.referencedEntityMetadata = options.referencedEntityMetadata;
Expand All @@ -90,6 +98,7 @@ export class ForeignKeyMetadata {
this.onDelete = options.onDelete || "NO ACTION";
this.onUpdate = options.onUpdate || "NO ACTION";
this.deferrable = options.deferrable;
this.givenName = options.name;
if (options.namingStrategy)
this.build(options.namingStrategy);
}
Expand All @@ -106,7 +115,7 @@ export class ForeignKeyMetadata {
this.columnNames = this.columns.map(column => column.databaseName);
this.referencedColumnNames = this.referencedColumns.map(column => column.databaseName);
this.referencedTablePath = this.referencedEntityMetadata.tablePath;
this.name = namingStrategy.foreignKeyName(this.entityMetadata.tableName, this.columnNames, this.referencedEntityMetadata.tableName, this.referencedColumnNames);
this.name = this.givenName ? this.givenName : namingStrategy.foreignKeyName(this.entityMetadata.tableName, this.columnNames, this.referencedEntityMetadata.tableName, this.referencedColumnNames);
}

}

0 comments on commit 6f623c2

Please sign in to comment.