Skip to content

Data Model

This document describes the physical data model for TyteRide as stated in the domain glossary and preserved build spec. It complements the conceptual model in ../01-domain/01-domain-model.md; use that document for aggregate meaning and ownership, and this document for table-level storage details.

1. Scope and source basis

Source What it provides Gaps
../domain/README.md §5 New/changed table inventory and key field names No full column types, nullability, FK names, or index DDL
../product/b2b-rework-plan.md §4 Field-level intent for new and changed tables Preserved spec; some legacy naming remains (see notes)
../architecture.md §3 Tenancy rules and CompanyScope expectations No physical schema

Where the source is silent, this document says Source silent rather than inventing columns, types, defaults, or indexes.

2. Entity relationship diagram

erDiagram
    SUBSCRIPTION_TIERS ||--o{ COMPANIES : "plans"
    COMPANIES ||--o{ COMPANY_MEMBERS : "has"
    USERS ||--o{ COMPANY_MEMBERS : "belongs to"
    COMPANIES ||--o{ COMPANY_CODES : "issues"
    COMPANIES ||--o{ VEHICLES : "owns record"
    COMPANIES ||--o{ NAV_ROUTES : "defines"
    COMPANIES ||--o{ TRIPS : "schedules"
    COMPANIES ||--o{ CAR_REQUESTS : "creates"
    NAV_ROUTES ||--o{ TRIPS : "used by"
    VEHICLES ||--o{ TRIPS : "assigned"
    VEHICLES ||--o{ CAR_REQUESTS : "assigned"
    TRIPS ||--o{ BOOKINGS : "contains"
    USERS ||--o{ BOOKINGS : "passenger"
    TRIPS ||--|| TRIP_ANALYTICS : "produces"
    TRIPS ||--o{ TRIP_REVIEWS : "receives"
    USERS ||--o{ TRIP_REVIEWS : "writes"
    CAR_REQUESTS ||--o{ CAR_REQUEST_PASSENGERS : "links"
    USERS ||--o{ CAR_REQUEST_PASSENGERS : "served passenger"

    SUBSCRIPTION_TIERS {
        bigint id PK
        string code
        string name_en
        string name_ar
        int min_users
        int max_users
        decimal_or_int monthly_price "source silent on exact type"
        timestamps timestamps "source silent"
    }

    COMPANIES {
        bigint id PK
        string name_en
        string name_ar
        string slug
        enum status "trial|active|suspended"
        bigint tier_id FK
        datetime subscription_ends_at "nullable, source silent"
        boolean d2d_enabled
        int d2d_max_detour_minutes "default 15"
        timestamps timestamps "source silent"
    }

    USERS {
        bigint id PK
        string locale "default en, added"
        string gender "added, required at enrollment"
        string role "existing global role; source silent on full shape"
        string phone "source silent; auth is phone+OTP"
        timestamps timestamps "source silent"
    }

    COMPANY_MEMBERS {
        bigint id PK
        bigint company_id FK
        bigint user_id FK
        enum role "manager|driver|passenger"
        enum status "pending|approved|rejected|suspended"
        bigint approved_by FK "nullable, source silent"
        boolean d2d_enabled
        timestamps timestamps "source silent"
    }

    COMPANY_CODES {
        bigint id PK
        bigint company_id FK
        string code
        datetime expires_at "nullable, source silent"
        int max_uses "nullable, source silent"
        int uses_count
        boolean is_active
        timestamps timestamps "source silent"
    }

    VEHICLES {
        bigint id PK
        bigint company_id FK "added; tenant scope"
        timestamps timestamps "source silent"
        other_fields "existing table; source silent"
    }

    NAV_ROUTES {
        bigint id PK
        bigint company_id FK "added; tenant scope"
        string name_ar "added"
        json_or_related stops "stop label_ar added; source silent on storage"
        timestamps timestamps "source silent"
        other_fields "existing table; source silent"
    }

    TRIPS {
        bigint id PK
        bigint company_id FK "added; tenant scope"
        bigint nav_route_id FK "existing; source silent"
        bigint vehicle_id FK "existing; source silent"
        bigint driver_id FK "existing; source silent on exact column"
        datetime reservation_deadline_at "nullable, added"
        string trip_group_code "nullable, added"
        timestamps timestamps "source silent"
        other_fields "existing table; source silent"
    }

    BOOKINGS {
        bigint id PK
        bigint trip_id FK "existing"
        bigint user_id FK "existing; passenger"
        enum pickup_type "route_stop|d2d, added"
        json d2d_pickup "lat,lng,label; nullable, added"
        json d2d_dropoff "lat,lng,label; nullable, added"
        datetime boarded_at "nullable, added"
        timestamps timestamps "source silent"
        other_fields "existing table; source silent"
    }

    CAR_REQUESTS {
        bigint id PK
        bigint company_id FK
        bigint requested_by FK "manager"
        bigint vehicle_id FK
        bigint driver_id FK
        string reason "source silent on type/length"
        string status "source silent on enum values"
        timestamps timestamps "source silent"
    }

    CAR_REQUEST_PASSENGERS {
        bigint id PK
        bigint car_request_id FK
        bigint user_id FK "passenger; source silent on exact name"
        timestamps timestamps "source silent"
    }

    TRIP_ANALYTICS {
        bigint id PK
        bigint trip_id FK "unique"
        int overall_delay_minutes
        int late_departure_minutes
        json unexpected_stops
        json slow_segments
        int d2d_detour_minutes
        datetime computed_at
        timestamps timestamps "source silent"
    }

    TRIP_REVIEWS {
        bigint id PK
        bigint trip_id FK
        bigint user_id FK
        int rating "1-5"
        text comment "nullable, source silent"
        timestamps timestamps "source silent"
    }

Diagram note: Mermaid erDiagram field types are illustrative labels. Exact SQL types, nullability, defaults, and foreign-key constraints are source silent unless explicitly stated in the sources above.

3. New tables

Slice 2 implementation note: The API implements these tables as companies (status: pending | active | suspended, plus nullable notes text stored for Platform Admin review and not exposed in public onboarding responses), tiers (no monthly_price column; billing is invoice-only), memberships (status: pending | active), and company_codes (code, company_id, status). The tables below preserve the original build-spec field inventory; where names or status values differ, the implemented names above are authoritative. users.company_id mirrors the User's single Membership.

3.1 companies

Field Source detail Notes
name_en Build spec §4 Source silent on type/length.
name_ar Build spec §4 Arabic field uses _ar suffix.
slug Build spec §4 Source silent on uniqueness/index.
status Build spec §4 trial \| active \| suspended.
tier_id Build spec §4 FK to subscription_tiers.
subscription_ends_at Build spec §4 Source silent on nullable/index.
d2d_enabled Build spec §4 Company-level D2D eligibility flag.
d2d_max_detour_minutes Build spec §4 Default 15.

Tenancy: Root tenant table.
Indexes/constraints: Source silent.
Timestamps: Source silent.

3.2 subscription_tiers

Field Source detail Notes
code Build spec §4 Source silent on uniqueness.
name_en Build spec §4 —
name_ar Build spec §4 —
min_users Build spec §4 —
max_users Build spec §4 Seat approvals blocked above this limit.
monthly_price Build spec §4 Money; API conventions require no floats in transit. Exact storage type source silent.

Indexes/constraints: Source silent.
Timestamps: Source silent.

3.3 company_members

Field Source detail Notes
company_id Build spec §4 Tenant FK.
user_id Build spec §4 User FK.
role Build spec §4 manager \| driver \| passenger.
status Build spec §4 pending \| approved \| rejected \| suspended.
approved_by Build spec §4 Actor reference; source silent on FK target/table.
d2d_enabled Build spec §4 Per-member D2D eligibility.

Tenancy: Company-scoped membership. One company per user is a domain invariant; source silent on unique DB constraint.
Indexes/constraints: Source silent.
Timestamps: Source silent.

3.4 company_codes

Field Source detail Notes
company_id Build spec §4 Tenant FK.
code Build spec §4 Source silent on format/uniqueness scope.
expires_at Build spec §4 Nullable/behavior source silent beyond field presence.
max_uses Build spec §4 —
uses_count Build spec §4 —
is_active Build spec §4 Manager can rotate/disable codes.

Indexes/constraints: Source silent.
Timestamps: Source silent.

3.5 car_requests

Field Source detail Notes
company_id Build spec §4 Tenant FK.
requested_by Build spec §4 Manager reference.
vehicle_id Build spec §4 Company vehicle.
driver_id Build spec §4 Company driver.
reason Build spec §4 —
status Build spec §4 Source silent on enum values/lifecycle.

Indexes/constraints: Source silent.
Timestamps: Source silent.

3.6 car_request_passengers

Field Source detail Notes
car_request_id Build spec §4 FK to car_requests.
passenger/user reference Build spec §4 says "Passengers linked via car_request_passengers" Exact column name/type source silent.

Indexes/constraints: Source silent.
Timestamps: Source silent.

3.7 trip_analytics

Field Source detail Notes
trip_id Build spec §4 Unique; one analysis per trip.
overall_delay_minutes Build spec §4 —
late_departure_minutes Build spec §4 —
unexpected_stops Build spec §4 JSON.
slow_segments Build spec §4 JSON.
d2d_detour_minutes Build spec §4 Reported separately from driver-caused delay.
computed_at Build spec §4 —

Indexes/constraints: trip_id unique stated. Other indexes source silent.
Timestamps: Source silent.

3.8 trip_reviews

Field Source detail Notes
company_id Slice 8 Tenant FK; derived from Trip, indexed for Company-scoped Manager queries.
trip_id Build spec §4 FK to trips; cascade on Trip delete.
user_id Build spec §4 Passenger author FK to users.
driver_id Slice 8 FK to memberships; derived server-side from trips.driver_id.
rating Build spec §4 Unsigned tiny integer, 1–5.
comment Build spec §4 Nullable text; optional, max 1000 chars enforced at application boundary.

Indexes/constraints: Unique (trip_id, user_id) enforces one review per Passenger per Trip. Indexes on (company_id, created_at), (company_id, rating), (company_id, trip_id), (company_id, driver_id).
Timestamps: Laravel created_at/updated_at.

4. Changed tables

4.1 users

Change Source detail Notes
add locale Build spec §4 Default en. Returned to clients per API conventions.
add gender Build spec §4 Required at enrollment; stored once on users.

Existing fields: Source silent in these docs; preserve existing auth/identity behavior.

4.2 nav_routes

Change Source detail Notes
add company_id Build spec §4 Tenant scope.
add name_ar Build spec §4 Arabic field.
stop labels get label_ar Build spec §4 Storage shape for stops/labels source silent.
add origin_lat, origin_lng, destination_lat, destination_lng Slice 7 Nullable decimal coordinate pairs for route endpoints.
add active Slice 7 Boolean, default true; inactive routes cannot be assigned to new trips.

Tenancy: CompanyScope applies.
Backfill: Existing rows backfilled into one seed company (build spec §4).

4.3 vehicles

Change Source detail Notes
add company_id Build spec §4 Tenant scope; vehicles are virtual company records.
add plate Slice 7 Nullable string(50); company-unique when present, normalized uppercase trim.
add type Slice 7 Nullable string(50) free-form label.
add icon Slice 7 Nullable string(100) UI icon key.

Tenancy: CompanyScope applies.
Backfill: Existing rows backfilled into one seed company.

4.4 trips

Change Source detail Notes
add company_id Build spec §4 Tenant scope.
add reservation_deadline_at Build spec §4 Nullable. Booking after deadline rejected.
add trip_group_code Build spec §4 Nullable; groups trips with same route/departure time, one per car.

Tenancy: CompanyScope applies.
Backfill: Existing rows backfilled into one seed company.

4.5 bookings

Change Source detail Notes
add pickup_type Build spec §4 route_stop \| d2d.
add d2d_pickup Build spec §4 JSON: lat, lng, label.
add d2d_dropoff Build spec §4 JSON: lat, lng, label.
add boarded_at Build spec §4 Nullable timestamp set by tap/QR boarding.

Tenancy: Slice 4 adds direct bookings.company_id; it must equal trip.company_id, and CompanyScope applies. Earlier source-silent notes about Trip-only scope are superseded by the implemented Slice 4 contract.

5. Tenancy and integrity rules

Rule Source Model implication
Company is the tenant Architecture §3, Domain §5 company_id on company-owned records; CompanyScope on nav_routes, vehicles, trips.
Bookings scoped through trip Architecture §3, API conventions §5 Booking queries must scope by trip.company_id.
Cross-tenant access returns 404 API conventions §5 Data access must not leak existence across tenants.
One company per user Domain §3 invariant Membership/user-company uniqueness should be enforced; exact constraint source silent.
Seat = approved passenger membership Domain §3 invariant Seat count is derived from approved passenger memberships.
Trip analysis one per trip Build spec §4 trip_analytics.trip_id unique.
No passenger payment path Domain §3 invariant No new payment/wallet/top-up tables or fields.

6. Legacy and migration notes

  • The preserved build spec references legacy app names in some endpoint examples (for example /coordinator/*). TyteRide naming uses /driver/* and /passenger/*; see 05-client-service-contract.md.
  • Existing rows for company-scoped tables are backfilled into one seed company during migration, per build spec §4.
  • Old price/payment columns and wallet endpoints may remain for history only; no new Passenger payment model is added.
  • Physical column types, foreign-key ON DELETE behavior, composite indexes, and storage engines are source silent unless a later implementation section explicitly defines them.

7. Slice 10 notifications

8.1 device_tokens

Field Type / constraints
user_id FK users, cascade delete, indexed with updated_at
company_id nullable FK companies, null on delete, indexed with platform
platform string(20): android, ios, web
token string(512), unique
last_seen_at nullable timestamp
revoked_at nullable timestamp (reserved)
timestamps created_at, updated_at

8.2 notifications

Field Type / constraints
user_id FK users, cascade delete
company_id nullable FK companies, null on delete
type string(100) machine-readable key
title string(255)
body text
data json object, default {}
read_at nullable timestamp
timestamps created_at, updated_at

Indexes: (user_id, read_at, created_at), (user_id, created_at), (company_id, created_at). Notifications are user-owned read models; cross-user reads return 404.

8. Slice 4 implemented Trip/Booking schema

The core Trip slice creates these tables. All use Laravel bigint IDs and timestamps; datetime columns are nullable only where shown.

7.1 nav_routes

Field Type / constraints
company_id bigint FK → companies, cascade delete, indexed
name_en, name_ar string(255)
origin, destination string(255)
origin_lat, origin_lng, destination_lat, destination_lng nullable decimal(10,7) coordinate pairs
stops nullable JSON ordered stops {name, name_ar?, lat?, lng?}
active boolean default true, indexed with company

7.2 vehicles

Field Type / constraints
company_id bigint FK → companies, cascade delete, indexed
name string(255)
plate nullable string(50); normalized uppercase, company-unique when present
seat_capacity unsigned small integer; API field capacity
type nullable string(50)
icon nullable string(100)
status string(20), default active (active\|inactive)

8.3 trips

Field Type / constraints
company_id bigint FK → companies, cascade delete, indexed
nav_route_id bigint FK → nav_routes, restrict delete
origin, destination string(255) route snapshots
departure_at datetime
vehicle_id bigint FK → vehicles, restrict delete
driver_id bigint FK → memberships; active Driver Membership, restrict delete
seat_capacity unsigned small integer; positive and ≤ Vehicle capacity
reservation_deadline_at nullable datetime; ≤ departure_at
trip_group_code nullable indexed string(100)
notes nullable text
status string(20), default scheduled; scheduled\|in_transit\|completed\|cancelled
started_at, completed_at, cancelled_at nullable datetimes

Indexes: (company_id, status, departure_at) and (company_id, driver_id, departure_at).

8.4 bookings

Field Type / constraints
company_id bigint FK → companies, cascade delete, indexed
trip_id bigint FK → trips, cascade delete
user_id bigint FK → users, cascade delete; Passenger
status string(20), default pending; pending\|approved\|rejected\|cancelled
rejection_reason nullable text
decided_at, cancelled_at, boarded_at nullable datetimes
created_at, updated_at timestamps

Indexes: (trip_id, status), (trip_id, user_id, status), and (company_id, user_id, status). There is no all-status unique Booking constraint because a Passenger may re-request after rejection/cancellation. One-active-Booking and no-overbooking are enforced transactionally under a Trip-row lock.

8.5 Derived seats

There is no seats or trip_seats table in Slice 4. Subscription Seat count is active Passenger Memberships; Trip seats_left is seat_capacity - count(approved Bookings). booked_seats is not persisted.