Skip to main content

Overview

The team_user table is a pivot table that manages the many-to-many relationship between teams and users. It tracks which users are members of which teams.

Schema

Columns

id (Primary Key)

  • Type: BIGINT UNSIGNED
  • Description: Unique identifier for the membership
  • Auto-increment: Yes

team_id

  • Type: BIGINT UNSIGNED
  • Description: Foreign key to teams table
  • Required: Yes
  • Constraints:
    • Foreign key to teams.id
    • Cascade on delete

user_id

  • Type: BIGINT UNSIGNED
  • Description: Foreign key to users table
  • Required: Yes
  • Constraints:
    • Foreign key to users.id
    • Cascade on delete

created_at / updated_at

  • Type: TIMESTAMP
  • Description: When user joined the team
  • Automatically managed: Yes

Unique Constraints

Composite Unique Index

Prevents duplicate memberships:
This ensures a user can only be a member of a team once.

Usage Examples

Adding a Member to a Team

Removing a Member from a Team

Checking Membership

Getting Team Members

Getting User’s Teams

Query Performance

Eager Loading

Always eager load relationships to avoid N+1 queries:

Indexes

The foreign keys automatically create indexes, but you can add more:

Adding Additional Pivot Data

You can add extra columns to store membership-specific data:

Migration

Model Configuration

Usage

Cascade Behavior

When teams or users are deleted:

Team Deleted

All team_user records for that team are automatically deleted:

User Deleted

All team_user records for that user are automatically deleted:

Testing

Next Steps