- Node.js installed
- PostgreSQL server running
- Basic knowledge of TypeScript and TypeORM
A one-to-one relationship connects one record in a table to exactly one record in another table.
A user has one profile, and a profile belongs to one user.
- Create src/entity/User.ts:
- Create src/entity/Profile.ts:
This example enhances the one-to-one relationship by adding an explicit foreign key column with an index and a named foreign key constraint.
- Modify src/entity/User.ts:
- Keep src/entity/Profile.ts as above.
The @JoinColumn decorator is used on the User entity to indicate that the user table owns the foreign key (profileId) referencing the profile table. In the advanced example:
- Indexing with @Index('idx_user_profile_id'): The @Index decorator creates an index on the profileId column, improving query performance for operations like filtering or joining on profileId (e.g., SELECT * FROM user WHERE profileId = 1). The named index (idx_user_profile_id) is identifiable in database tools for performance tuning.
- Named Foreign Key Constraint with foreignKeyConstraintName: The @JoinColumn specifies foreignKeyConstraintName: 'fk_user_profile_id', naming the foreign key constraint in the database. This makes debugging easier, as constraint violation errors will reference fk_user_profile_id.
- Cascade Option (cascade: true): The cascade: true option ensures that saving a User entity also saves its related Profile entity.
On the Profile side, @JoinColumn is not needed because the foreign key is managed by the User entity.
A one-to-many relationship allows one record in a table to be associated with multiple records in another table.
A user can have multiple posts, but each post belongs to one user.
- Modify src/entity/User.ts:
- Create src/entity/Post.ts:
This example enhances the many-to-one relationship by adding an explicit foreign key column with an index and a named foreign key constraint.
-
Keep src/entity/User.ts as above.
-
Modify src/entity/Post.ts:
In a one-to-many relationship, the foreign key is stored on the "many" side (e.g., the post table has a userId column). TypeORM automatically manages this foreign key, so @JoinColumn is not required in the basic example. In the advanced example:
- Indexing with @Index('idx_post_user_id'): The @Index decorator creates an index on the userId column, improving query performance for operations like filtering or joining on userId (e.g., SELECT * FROM post WHERE userId = 1). The named index (idx_post_user_id) is identifiable in database tools.
- Named Foreign Key Constraint with foreignKeyConstraintName: The @JoinColumn specifies foreignKeyConstraintName: 'fk_post_user_id', naming the foreign key constraint. This aids debugging by making constraint violation errors reference fk_post_user_id.
- Cascade Option (cascade: true): The cascade: true option ensures that saving a User entity also saves its related Post entities.
On the User side, @JoinColumn is not used because the user table does not store the foreign key; the relationship is managed by the post table.
A many-to-many relationship allows multiple records in one table to be associated with multiple records in another table, using a junction table.
A user can have multiple roles, and a role can be assigned to multiple users.
- Modify src/entity/User.ts:
- Create src/entity/Role.ts:
This example replaces the automatic junction table with an explicit entity (UserRole) to manage the many-to-many relationship, allowing additional attributes and enhanced indexing.
- Modify src/entity/User.ts:
- Modify `src/entityand Role with Explicit Junction Table Entity
- Create src/entity/UserRole.ts:
Below is an example of how to use the UserRole entity to manage the many-to-many relationship between User and Role, including creating, saving, and querying relationships with additional attributes.
In the basic many-to-many example, TypeORM creates an automatic junction table (e.g., user_roles) managed by @JoinTable. In the advanced example, we define an explicit UserRole entity to represent the junction table, allowing additional attributes (e.g., assignedAt) and fine-grained control:
- Explicit Junction Table with UserRole: The UserRole entity replaces the automatic junction table. It has its own primary key (id) and additional fields like assignedAt to store metadata about the relationship (e.g., when a role was assigned to a user). The User and Role entities now have one-to-many relationships with UserRole, which in turn has many-to-one relationships with User and Role.
- Indexing with @Index: Indexes are created on userId (idx_user_role_user_id) and roleId (idx_user_role_role_id) columns to improve query performance for operations like filtering or joining on these columns (e.g., SELECT * FROM user_role WHERE userId = 1). The named indexes are identifiable in database tools for performance tuning.
- Named Foreign Key Constraints with foreignKeyConstraintName: The @JoinColumn decorators specify foreignKeyConstraintName (fk_user_role_user_id and fk_user_role_role_id), naming the foreign key constraints. This aids debugging by making constraint violation errors reference these names.
- Cascade Option (cascade: true): The cascade: true option ensures that operations on User or Role entities (e.g., saving) cascade to related UserRole entities.
- Creating and Saving: The example creates a User ("Alice") and two Role entities ("Admin" and "Editor"). It then creates UserRole entities to link the user to each role, setting the assignedAt date to track when each role was assigned. The entities are saved using their respective repositories.
- Querying with Relations: The findOne query loads the user along with their userRoles and the related role entities, demonstrating how to access the roles and their assignment dates. The output logs each role and its assignment date.
- Advanced Querying: The example includes a query to find UserRole entries for a specific user where roles were assigned after a certain date, showcasing the benefit of the assignedAt attribute. This query uses the MoreThan operator from TypeORM to filter by date.
- Performance and Debugging: The indexes on userId and roleId improve the performance of these queries, especially when filtering or joining. The named foreign key constraints (fk_user_role_user_id and fk_user_role_role_id) ensure that any constraint violations (e.g., attempting to delete a User with existing UserRole entries) produce clear error messages.
This approach provides more flexibility than the automatic junction table, as it allows additional attributes and direct querying of the junction table (e.g., SELECT * FROM user_role WHERE assignedAt > '2025-01-01').
This guide provides a foundation for implementing relationships in TypeORM with PostgreSQL, with a focus on basic and advanced configurations using @JoinColumn, indexing, named constraints, and explicit junction table entities for performance and debugging.