Skip to content

Commit

Permalink
feat: allow explicitly named primary keys, foreign keys, and indices (#…
Browse files Browse the repository at this point in the history
…8900)

* feat: add constraintName to JoinColumn

Add a constraintName to JoinColumn decorators to allow specifying foreignKey name.
Use constraintName when building JoinTable entities as well.

Partially solves: #1355

* test: add tests for constraintNames on JoinColumn

* docs: add constraintName documentation to JoinColumn and JoinTable

* test: update snapshot in 5444 test

Add constraintName property with correct variable undefined to snapshot in tests for issue 5444.

* prettier

* added support for custom FK name in Sqlite;
added test;

* removed .only

* fixed FK constraint renaming on table/column rename

* minor fix

* fixed @unique and @Index constraints renaming on table/column rename

* working on constraint name support for PK

* replaced `constraintName` with `primaryKeyConstraintName` and `foreignKeyConstraintName`

* fixed failing test

* working on constraint name support for PK

* updated docs

Co-authored-by: Matthijs Hatzmann <matthijs.hatzmann@tradecast.eu>
  • Loading branch information
AlexMesser and M-TGH committed Apr 29, 2022
1 parent 9f8429f commit 78df84c
Show file tree
Hide file tree
Showing 43 changed files with 2,058 additions and 346 deletions.
33 changes: 31 additions & 2 deletions docs/decorator-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export class User {
- `enum: string[]|AnyEnum` - Used in `enum` column type to specify list of allowed enum values.
You can specify array of values or specify a enum class.
- `enumName: string` - A name for generated enum type. If not specified, TypeORM will generate a enum type from entity and column names - so it's necessary if you intend to use the same enum type in different tables.
- `primaryKeyConstraintName: string` - A name for the primary key constraint. If not specified, then constraint name is generated from the table name and the names of the involved columns.
- `asExpression: string` - Generated column expression. Used only in [MySQL](https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html) and [Postgres](https://www.postgresql.org/docs/12/ddl-generated-columns.html).
- `generatedType: "VIRTUAL"|"STORED"` - Generated column type. Used only in [MySQL](https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html) and [Postgres (Only "STORED")](https://www.postgresql.org/docs/12/ddl-generated-columns.html).
- `hstoreType: "object"|"string"` - Return type of `HSTORE` column. Returns value as string or as object. Used only in [Postgres](https://www.postgresql.org/docs/9.6/static/hstore.html).
Expand All @@ -213,6 +214,7 @@ Learn more about [entity columns](entities.md#entity-columns).

Marks a property in your entity as a table primary column.
Same as `@Column` decorator but sets its `primary` option to true.

Example:

```typescript
Expand All @@ -223,6 +225,18 @@ export class User {
}
```

`@PrimaryColumn()` supports custom primary key constraint name:

```typescript
@Entity()
export class User {
@PrimaryColumn({ primaryKeyConstraintName: "pk_user_id" })
id: number
}
```

> Note: when using `primaryKeyConstraintName` with multiple primary keys, the constraint name must be the same for all primary columns.
Learn more about [entity columns](entities.md#entity-columns).

#### `@PrimaryGeneratedColumn`
Expand All @@ -239,6 +253,16 @@ export class User {
}
```

`@PrimaryGeneratedColumn()` supports custom primary key constraint name:

```typescript
@Entity()
export class User {
@PrimaryGeneratedColumn({ primaryKeyConstraintName: "pk_user_id" })
id: number
}
```

There are four generation strategies:

- `increment` - uses AUTO_INCREMENT / SERIAL / SEQUENCE (depend on database type) to generate incremental number.
Expand Down Expand Up @@ -464,7 +488,7 @@ Learn more about [many-to-many relations](many-to-many-relations.md).
#### `@JoinColumn`

Defines which side of the relation contains the join column with a foreign key and
allows you to customize the join column name and referenced column name.
allows you to customize the join column name, referenced column name and foreign key name.
Example:

```typescript
Expand All @@ -474,6 +498,7 @@ export class Post {
@JoinColumn({
name: "cat_id",
referencedColumnName: "name",
foreignKeyConstraintName: "fk_cat_id"
})
category: Category
}
Expand All @@ -483,7 +508,9 @@ export class Post {

Used for `many-to-many` relations and describes join columns of the "junction" table.
Junction table is a special, separate table created automatically by TypeORM with columns referenced to the related entities.
You can change the name of the generated "junction" table and also the column names inside the junction table and their referenced columns with the `joinColumn`- and `inverseJoinColumn` attributes.
You can change the name of the generated "junction" table, the column names inside the junction table, their referenced
columns with the `joinColumn`- and `inverseJoinColumn` attributes, and the created foreign keys names.

Example:

```typescript
Expand All @@ -495,10 +522,12 @@ export class Post {
joinColumn: {
name: "question",
referencedColumnName: "id",
foreignKeyConstraintName: "fk_question_categories_questionId"
},
inverseJoinColumn: {
name: "category",
referencedColumnName: "id",
foreignKeyConstraintName: "fk_question_categories_categoryId"
},
})
categories: Category[]
Expand Down
11 changes: 11 additions & 0 deletions src/decorator/options/ColumnOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,22 @@ export interface ColumnOptions extends ColumnCommonOptions {
* Array of possible enumerated values.
*/
enum?: (string | number)[] | Object

/**
* Exact name of enum
*/
enumName?: string

/**
* If this column is primary key then this specifies the name for it.
*/
primaryKeyConstraintName?: string

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

/**
* Generated column expression.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/decorator/options/JoinColumnOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export interface JoinColumnOptions {
* Name of the column in the entity to which this column is referenced.
*/
referencedColumnName?: string // TODO rename to referencedColumn

/**
* Name of the foreign key constraint.
*/
foreignKeyConstraintName?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ export interface PrimaryGeneratedColumnIdentityOptions {
* Identity column type. Supports only in Postgres 10+.
*/
generatedIdentity?: "ALWAYS" | "BY DEFAULT"

/**
* Name of the primary key constraint.
*/
primaryKeyConstraintName?: string
}
5 changes: 5 additions & 0 deletions src/decorator/options/PrimaryGeneratedColumnNumericOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ export interface PrimaryGeneratedColumnNumericOptions {
* Puts UNSIGNED attribute on to numeric column. Works only for MySQL.
*/
unsigned?: boolean

/**
* Name of the primary key constraint.
*/
primaryKeyConstraintName?: string
}
5 changes: 5 additions & 0 deletions src/decorator/options/PrimaryGeneratedColumnUUIDOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export interface PrimaryGeneratedColumnUUIDOptions {
* Column comment. Not supported by all database types.
*/
comment?: string

/**
* Name of the primary key constraint.
*/
primaryKeyConstraintName?: string
}
1 change: 1 addition & 0 deletions src/decorator/relations/JoinColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function JoinColumn(
propertyName: propertyName,
name: options.name,
referencedColumnName: options.referencedColumnName,
foreignKeyConstraintName: options.foreignKeyConstraintName,
} as JoinColumnMetadataArgs)
})
}
Expand Down

0 comments on commit 78df84c

Please sign in to comment.