Vacation Rental Marketplace ER diagram and database schema

The Vacation Rental Marketplace database schema is a DB Planner template with 24 tables, 214 columns and 40 relationships, free to use. Core tables include amenities, booking_charges, booking_nights, bookings. It can be opened as an interactive ER diagram or exported as PostgreSQL, MySQL, or SQL Server DDL.

ER diagram of the Vacation Rental Marketplace database schema, showing 24 tables and their relationships
FIG. 1: entity relationship diagram, 24 tables
Tables
24
Columns
214
Relationships
40
Category
Marketplace
Exports
DBML, SQL, Prisma, C#
Price
Free

What this schema models

A complete vacation rental marketplace database design, built the way short-term rental platforms actually run: 24 tables, 17 enums and 40 relationships covering hosts and guests, identity checks and wishlists, listings with photos, amenities, house rules and versioned cancellation policies, a per-night availability calendar with pricing rules and iCal sync, instant and request-to-book bookings with per-night price snapshots, guest payments, refunds and host payouts, two-sided reviews and message threads. THE FOUR DESIGN RULES 1. One row per listing per NIGHT. calendar_nights is keyed on (listing_id, night), and a booking claims its nights with one conditional UPDATE that must touch exactly as many rows as nights booked. A night carries only one booking_id, so two overlapping bookings can never both commit. A night is the date you sleep, so check-out day stays free for the next check-in. 2. Prices are snapshotted per night. booking_nights stores each night's price and the rule that produced it, and every fee, discount and tax is a booking_charges line. Editing a weekend or seasonal rule never changes a confirmed stay, and "why was Saturday more?" has an answer. 3. Guest money and host money are separate. Guest payments follow the guest's schedule (pay part now, part later); host payouts are released about 24 hours after check-in. The platform holds the funds in between, so a cancellation before arrival is a refund to the guest, never a clawback from the host. 4. Reviews are blind until both are in. When the first review of a stay arrives, its published_at is set to the review deadline; when the second arrives, both are published at once. Readers filter on published_at <= now(), so neither side reads the other first and no job has to run at the deadline. DETAILS WORTH STEALING - Cancellation policies are versioned rows, never edited. A booking points at the exact version it was made under, and the cancellation stores the refund it computed. - Pricing resolves in a fixed order: calendar override, then the highest-priority seasonal or weekend rule, then the base price, with length-of-stay, early bird and last minute discounts applied to the subtotal. - Instant book and request to book share one bookings table; a request holds its nights and auto-declines after 24 hours. - Exact coordinates are shown only to confirmed guests, and messages sent before confirmation have contact details masked to keep payment on the platform. - iCal import and export feeds turn busy dates from other sites into blocked nights. - Hosts and guests are one users table, because most hosts travel too. WHAT YOU GET Clean DBML that imports directly into DB Planner, and a laid-out board with zones color-coded and framed: users and trust, listings, availability and pricing, bookings, payments and payouts, and reviews and messaging. Every table carries a note. Postgres-flavoured types that port cleanly to MySQL or SQL Server. Useful for developers building a vacation rental, holiday let or short-term booking marketplace, and for students who need a realistic vacation rental ER diagram for a DBMS project. This is a schema design reference; occupancy taxes, local short-term rental rules and payment regulation vary by market and belong in your own business logic.

Tables (24)

Every table and its columns

users

Everyone on the marketplace. A host is simply a user with listings, and most hosts travel as guests too.

Columns of the users table
ColumnTypeConstraintsReferencesDescription
iduuidPK
emailvarchar(254)unique, not null
first_namevarchar(100)not null
last_namevarchar(100)not null
phonevarchar(30)
abouttext
avatar_storage_keyvarchar(300)
preferred_currencychar(3)not null, default 'USD'
host_sincedateSet when the first listing goes live. Hosts and guests are the same users.
created_attimestamptznot null, default `now()`
deleted_attimestamptzSoft delete: past bookings and reviews must still name both sides

identity_verifications

Trust checks: email, phone, government ID. Hosts can require a verified ID before accepting a booking.

Columns of the identity_verifications table
ColumnTypeConstraintsReferencesDescription
iduuidPK
user_iduuidFK, not nullusers.id
kindverification_kindnot null
statusverification_statusnot null, default 'pending'
provider_referencevarchar(100)Verification vendor reference; document images stay with the vendor
verified_attimestamptz
expires_attimestamptz

Indexes: (user_id, kind)

saved_listings

Wishlists.

Columns of the saved_listings table
ColumnTypeConstraintsReferencesDescription
user_iduuidFK, not nullusers.id
listing_iduuidFK, not nulllistings.id
list_namevarchar(100)not null, default 'Favorites'e.g. Summer 2027, Ski trip
saved_attimestamptznot null, default `now()`

listings

A place to stay, owned by one host. Base price, cleaning fee and guest limits live here; adjustments live in pricing_rules and calendar_nights.

Columns of the listings table
ColumnTypeConstraintsReferencesDescription
iduuidPK
host_iduuidFK, not nullusers.id
titlevarchar(150)not null
descriptiontextnot null
room_typeroom_typenot null
property_typevarchar(50)not nulle.g. Apartment, Cabin, Villa, Houseboat
address_line1varchar(200)not null
cityvarchar(120)not null
countrychar(2)not null
latitudenumeric(9,6)not nullExact point is shown only to confirmed guests; search shows a blurred area
longitudenumeric(9,6)not null
timezonevarchar(64)not nullIANA zone. Check-in times and calendar nights are local to the property.
max_guestssmallintnot null
bedroomssmallintnot null
bedssmallintnot null
bathroomsnumeric(3,1)not null1.5 means one full and one half bath
currencychar(3)not null
base_nightly_centsintegernot nullDefault price for a night with no rule or override
cleaning_fee_centsintegernot null, default 0Charged once per stay
guests_includedsmallintnot null, default 1
extra_guest_fee_centsintegernot null, default 0Per night, for each guest above guests_included
min_nightssmallintnot null, default 1
max_nightssmallint
check_in_fromtimenot null
check_out_bytimenot null
booking_modebooking_modenot null, default 'request'
cancellation_policy_iduuidFK, not nullcancellation_policies.id
statuslisting_statusnot null, default 'draft'
created_attimestamptznot null, default `now()`

Indexes: (host_id), (country, city, status)

listing_photos

Listing photos. Dimensions are stored so the page can reserve space before images load.

Columns of the listing_photos table
ColumnTypeConstraintsReferencesDescription
iduuidPK
listing_iduuidFK, not nulllistings.id
storage_keyvarchar(300)unique, not nullObject storage key. Image bytes never live in the database.
captionvarchar(200)
positionsmallintnot nullDisplay order; the first is the cover photo
widthintegernot null
heightintegernot null
created_attimestamptznot null, default `now()`

Indexes: (listing_id, position)

amenities

The fixed amenity catalog. Search filters by code, so a typo by one host never splits a filter.

Columns of the amenities table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codevarchar(40)unique, not nulle.g. wifi, kitchen, hot_tub, ev_charger, step_free_entrance
namevarchar(100)not null
categoryamenity_categorynot null

listing_amenities

Which amenities a listing offers.

Columns of the listing_amenities table
ColumnTypeConstraintsReferencesDescription
listing_iduuidFK, not nulllistings.id
amenity_iduuidFK, not nullamenities.id
detailsvarchar(200)e.g. 500 Mbps, shared with one other unit

Indexes: (amenity_id, listing_id)

house_rules

Rules the guest must accept before booking. The booking records when they were accepted.

Columns of the house_rules table
ColumnTypeConstraintsReferencesDescription
iduuidPK
listing_iduuidFK, not nulllistings.id
rulehouse_rule_kindnot null
is_allowedbooleannot nulle.g. pets: true, smoking: false
detailsvarchar(300)e.g. Quiet hours 22:00 to 08:00, one dog under 20 kg

Indexes: (listing_id)

cancellation_policies

Versioned rows, never edited. Changing a policy inserts a new version; a booking points at the exact version it was made under, so the refund a guest was promised can always be recomputed.

Columns of the cancellation_policies table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codevarchar(30)not nulle.g. flexible, moderate, strict, non_refundable
versionsmallintnot null
namevarchar(100)not null
full_refund_days_beforesmallintnot nullFull refund when cancelled at least this many days before check-in
partial_refund_days_beforesmallintPartial refund window, if any
partial_refund_pctsmallintShare of the nightly subtotal refunded in the partial window
cleaning_fee_refundablebooleannot null, default trueRefunded whenever the stay has not started
effective_fromtimestamptznot null

Indexes: (code, version) unique

calendar_nights

One row per listing per night, generated for the bookable window (rolled forward daily). A booking claims its nights with one conditional UPDATE: SET status = booked, booking_id = $1 WHERE listing_id = $2 AND night >= check_in AND night < check_out AND status = available. It must touch exactly as many rows as nights booked, or the transaction rolls back. A night can carry only one booking_id, so two overlapping bookings can never both commit.

Columns of the calendar_nights table
ColumnTypeConstraintsReferencesDescription
listing_iduuidFK, not nulllistings.id
nightdatenot nullThe date the guest sleeps there. Check-out day is not a night, so back-to-back stays fit.
statusnight_statusnot null, default 'available'
booking_iduuidFKbookings.idSet while the night is held for a request or booked
price_override_centsintegerHost-set price for this date; beats every rule
min_nights_overridesmallinte.g. 3-night minimum over a holiday
updated_attimestamptznot null, default `now()`

Indexes: (booking_id)

pricing_rules

How a night is priced. Resolution order: calendar override, then the highest-priority seasonal or weekend rule, then base_nightly_cents. Length-of-stay, early bird and last minute rules then discount the nightly subtotal. The cleaning fee is not a rule; it is a fixed per-stay amount on the listing.

Columns of the pricing_rules table
ColumnTypeConstraintsReferencesDescription
iduuidPK
listing_iduuidFK, not nulllistings.id
kindpricing_rule_kindnot null
adjustment_pctnumeric(5,2)Negative for a discount, positive for a markup
nightly_price_centsintegerAlternative to a percentage: a fixed nightly price, e.g. for a season
start_datedateSeasonal rules
end_datedate
weekend_nightssmallintWeekend rules: bitmask of nights, Friday and Saturday by default
min_nightssmallintLength-of-stay rules: 7 for weekly, 28 for monthly
days_before_arrivalsmallintEarly bird (at least) and last minute (at most) rules
prioritysmallintnot null, default 0Highest wins when two nightly rules overlap
is_activebooleannot null, default true

Indexes: (listing_id, kind)

calendar_syncs

iCal feeds to and from other sites. Imported busy dates become blocked nights, which is how hosts list on several platforms without double bookings.

Columns of the calendar_syncs table
ColumnTypeConstraintsReferencesDescription
iduuidPK
listing_iduuidFK, not nulllistings.id
directionsync_directionnot null
feed_urlvarchar(500)not nulliCal URL. Treated as a secret: anyone with it can read the calendar.
last_synced_attimestamptz
last_errorvarchar(300)

bookings

A stay. Totals here are the sum of booking_nights and booking_charges, frozen when the guest pays.

Columns of the bookings table
ColumnTypeConstraintsReferencesDescription
iduuidPK
confirmation_codevarchar(12)unique, not null
listing_iduuidFK, not nulllistings.id
guest_iduuidFK, not nullusers.id
host_iduuidFK, not nullusers.idCopied from the listing, so a listing changing hands never rewrites past stays
modebooking_modenot nullInstant books confirm at once; requests wait for the host
statusbooking_statusnot null
check_indatenot null
check_outdatenot nullCHECK (check_out > check_in)
adultssmallintnot null
childrensmallintnot null, default 0
infantssmallintnot null, default 0
petssmallintnot null, default 0
currencychar(3)not null
guest_total_centsintegernot nullWhat the guest pays: nights plus guest-side charges
host_payout_centsintegernot nullWhat the host earns: nights plus host-side charges, net of the host fee
cancellation_policy_iduuidFK, not nullcancellation_policies.idThe exact policy version in force at booking
house_rules_accepted_attimestamptznot null
request_expires_attimestamptzRequests auto-decline after 24 hours and release their nights
created_attimestamptznot null, default `now()`
confirmed_attimestamptz

Indexes: (listing_id, check_in), (guest_id, check_in)

booking_nights

The price of every night, as quoted. Editing rules later never changes a confirmed stay, and "why was Saturday more?" has an answer.

Columns of the booking_nights table
ColumnTypeConstraintsReferencesDescription
booking_iduuidFK, not nullbookings.id
nightdatenot null
price_centsintegernot nullSnapshot of the resolved nightly price
pricing_rule_iduuidFKpricing_rules.idThe rule that set it; null for base price or a calendar override

booking_charges

Every line on both receipts: cleaning fee, extra guests, pets, discounts, service fees and taxes.

Columns of the booking_charges table
ColumnTypeConstraintsReferencesDescription
iduuidPK
booking_iduuidFK, not nullbookings.id
kindcharge_kindnot null
payerpartynot nullGuest-side lines add to the guest total; host-side lines reduce the payout
descriptionvarchar(200)not nulle.g. Weekly discount 10%, Cleaning fee, Occupancy tax 6%
amount_centsintegernot nullNegative for discounts

cancellations

The outcome of a cancellation. The money math is stored, not recomputed later, so a policy change can never alter it.

Columns of the cancellations table
ColumnTypeConstraintsReferencesDescription
booking_iduuidPK, FKbookings.id
cancelled_bypartynot null
cancelled_by_user_iduuidFKusers.idNull when support cancels on behalf of either side
reasonvarchar(300)
guest_refund_centsintegernot nullComputed from the policy version on the booking, then frozen
host_payout_kept_centsintegernot nullWhat the host still earns after the refund
host_penalty_centsintegernot null, default 0Charged when the host cancels a confirmed stay
cancelled_attimestamptznot null, default `now()`

payment_methods

Guest cards, as processor tokens.

Columns of the payment_methods table
ColumnTypeConstraintsReferencesDescription
iduuidPK
user_iduuidFK, not nullusers.id
processor_tokenvarchar(200)not nullCard data stays with the payment processor; only its token is stored
brandvarchar(20)not null
last4char(4)not null
expires_ondatenot null

guest_payments

Money in, from the guest, on the guest schedule.

Columns of the guest_payments table
ColumnTypeConstraintsReferencesDescription
iduuidPK
booking_iduuidFK, not nullbookings.id
payment_method_iduuidFK, not nullpayment_methods.id
installmentsmallintnot null, default 1Pay part now, part later: 1 at booking, 2 before arrival
amount_centsintegernot null
currencychar(3)not null
statuspayment_statusnot null, default 'scheduled'
due_attimestamptznot null
processed_attimestamptz
processor_referencevarchar(100)unique

Indexes: (status, due_at)

refunds

Money back to the guest, against a specific payment, since a refund goes back to the card it came from.

Columns of the refunds table
ColumnTypeConstraintsReferences
iduuidPK
guest_payment_iduuidFK, not nullguest_payments.id
booking_iduuidFK, not nullbookings.id
amount_centsintegernot null
reasonrefund_reasonnot null
statuspayment_statusnot null, default 'scheduled'
processor_referencevarchar(100)
created_attimestamptznot null, default `now()`

payout_accounts

Where a host gets paid.

Columns of the payout_accounts table
ColumnTypeConstraintsReferencesDescription
iduuidPK
host_iduuidFK, not nullusers.id
processor_account_tokenvarchar(200)not nullConnected-account id; bank details stay with the processor
currencychar(3)not null
countrychar(2)not null
is_defaultbooleannot null, default false
verified_attimestamptz

host_payouts

Money out, to the host, on the host schedule. Guest money and host money never share a row: the platform holds funds between the guest charge and the host release, so a cancellation before arrival is a refund to the guest, never a clawback from the host.

Columns of the host_payouts table
ColumnTypeConstraintsReferencesDescription
iduuidPK
booking_iduuidFK, not nullbookings.id
host_iduuidFK, not nullusers.id
payout_account_iduuidFK, not nullpayout_accounts.id
amount_centsintegernot nullNegative for an adjustment, e.g. a host cancellation penalty
currencychar(3)not null
statuspayout_statusnot null, default 'scheduled'
release_aftertimestamptznot nullAbout 24 hours after check-in, once the guest has arrived
paid_attimestamptz
processor_referencevarchar(100)unique

Indexes: (status, release_after)

reviews

Two-sided and blind. When the first review of a stay arrives, its published_at is set to the deadline (check-out plus 14 days). When the second arrives, both are set to now(). Readers filter on published_at <= now(), so neither side can read the other first, and nothing has to wake up at the deadline.

Columns of the reviews table
ColumnTypeConstraintsReferencesDescription
iduuidPK
booking_iduuidFK, not nullbookings.id
directionreview_directionnot null
author_iduuidFK, not nullusers.id
subject_iduuidFK, not nullusers.id
overall_ratingsmallintnot null1 to 5
cleanliness_ratingsmallintCategory ratings apply to guest-to-host reviews only
accuracy_ratingsmallint
communication_ratingsmallint
location_ratingsmallint
value_ratingsmallint
public_texttextnot null
private_feedbacktextSeen only by the other party, never published
submitted_attimestamptznot null, default `now()`
published_attimestamptznot nullWhen it becomes visible; see the table note

Indexes: (booking_id, direction) unique, (subject_id, published_at)

message_threads

One conversation between a guest and a host about a listing, attached to the booking once there is one.

Columns of the message_threads table
ColumnTypeConstraintsReferencesDescription
iduuidPK
listing_iduuidFK, not nulllistings.id
guest_iduuidFK, not nullusers.id
host_iduuidFK, not nullusers.id
booking_iduuidFKbookings.idNull for an inquiry before booking
last_message_attimestamptz
created_attimestamptznot null, default `now()`

Indexes: (guest_id, last_message_at), (host_id, last_message_at)

messages

Messages in a thread. Kept as the record if a dispute reaches support.

Columns of the messages table
ColumnTypeConstraintsReferencesDescription
iduuidPK
thread_iduuidFK, not nullmessage_threads.id
sender_iduuidFKusers.idNull for system messages, e.g. booking confirmed
bodytextnot null
was_redactedbooleannot null, default falsePhone numbers and emails are masked before a booking is confirmed, to keep payment on the platform
sent_attimestamptznot null, default `now()`
read_attimestamptz

Indexes: (thread_id, sent_at)

Enums (17)

verification_kind
email, phone, government_id
verification_status
pending, verified, failed, expired
room_type
entire_place, private_room, shared_room
listing_status
draft, pending_review, listed, unlisted, suspended
booking_mode
instant, request
amenity_category
essentials, kitchen, bathroom, safety, outdoor, parking, accessibility, entertainment
house_rule_kind
pets, smoking, parties, children, quiet_hours, other
night_status
available, blocked, held, booked
pricing_rule_kind
weekend, seasonal, length_of_stay, early_bird, last_minute
sync_direction
import, export
booking_status
requested, declined, expired, confirmed, cancelled, completed
charge_kind
cleaning_fee, extra_guest_fee, pet_fee, discount, service_fee, tax
party
guest, host, platform
payment_status
scheduled, succeeded, failed, cancelled
refund_reason
guest_cancellation, host_cancellation, listing_issue, goodwill
payout_status
scheduled, released, paid, failed, reversed
review_direction
guest_to_host, host_to_guest

Relationships (40)

  • booking_charges.booking_id references bookings.id many to one
  • booking_nights.booking_id references bookings.id many to one
  • booking_nights.pricing_rule_id references pricing_rules.id many to one
  • bookings.cancellation_policy_id references cancellation_policies.id many to one
  • bookings.guest_id references users.id many to one
  • bookings.host_id references users.id many to one
  • bookings.id references cancellations.booking_id one to one
  • bookings.listing_id references listings.id many to one
  • calendar_nights.booking_id references bookings.id many to one
  • calendar_nights.listing_id references listings.id many to one
  • calendar_syncs.listing_id references listings.id many to one
  • cancellations.cancelled_by_user_id references users.id many to one
  • guest_payments.booking_id references bookings.id many to one
  • guest_payments.payment_method_id references payment_methods.id many to one
  • host_payouts.booking_id references bookings.id many to one
  • host_payouts.host_id references users.id many to one
  • host_payouts.payout_account_id references payout_accounts.id many to one
  • house_rules.listing_id references listings.id many to one
  • identity_verifications.user_id references users.id many to one
  • listing_amenities.amenity_id references amenities.id many to one
  • listing_amenities.listing_id references listings.id many to one
  • listing_photos.listing_id references listings.id many to one
  • listings.cancellation_policy_id references cancellation_policies.id many to one
  • listings.host_id references users.id many to one
  • message_threads.booking_id references bookings.id many to one
  • message_threads.guest_id references users.id many to one
  • message_threads.host_id references users.id many to one
  • message_threads.listing_id references listings.id many to one
  • messages.sender_id references users.id many to one
  • messages.thread_id references message_threads.id many to one
  • payment_methods.user_id references users.id many to one
  • payout_accounts.host_id references users.id many to one
  • pricing_rules.listing_id references listings.id many to one
  • refunds.booking_id references bookings.id many to one
  • refunds.guest_payment_id references guest_payments.id many to one
  • reviews.author_id references users.id many to one
  • reviews.booking_id references bookings.id many to one
  • reviews.subject_id references users.id many to one
  • saved_listings.listing_id references listings.id many to one
  • saved_listings.user_id references users.id many to one

Vacation Rental Marketplace database schema in DBML

The schema as it is written: DBML, the text format DB Planner draws its diagrams from. Copy it into any DBML tool, or open it in the diagram above.

vacation-rental-marketplace.dbml dbml
// Vacation Rental Marketplace
// Zones (header colors): users and trust (gray), listings (blue),
// availability and pricing (teal), bookings (amber), payments and payouts (purple),
// reviews and messaging (green).

// ---------------------------------------------------------------------------
// USERS AND TRUST
// ---------------------------------------------------------------------------

Table users [headercolor: #90a4ae] {
  id uuid [pk]
  email varchar(254) [not null, unique]
  first_name varchar(100) [not null]
  last_name varchar(100) [not null]
  phone varchar(30)
  about text
  avatar_storage_key varchar(300)
  preferred_currency char(3) [not null, default: 'USD']
  host_since date [note: 'Set when the first listing goes live. Hosts and guests are the same users.']
  created_at timestamptz [not null, default: `now()`]
  deleted_at timestamptz [note: 'Soft delete: past bookings and reviews must still name both sides']
  Note: 'Everyone on the marketplace. A host is simply a user with listings, and most hosts travel as guests too.'
}

Table identity_verifications [headercolor: #90a4ae] {
  id uuid [pk]
  user_id uuid [not null, ref: > users.id]
  kind verification_kind [not null]
  status verification_status [not null, default: 'pending']
  provider_reference varchar(100) [note: 'Verification vendor reference; document images stay with the vendor']
  verified_at timestamptz
  expires_at timestamptz

  indexes {
    (user_id, kind)
  }

  Note: 'Trust checks: email, phone, government ID. Hosts can require a verified ID before accepting a booking.'
}

Table saved_listings [headercolor: #90a4ae] {
  user_id uuid [not null, ref: > users.id]
  listing_id uuid [not null, ref: > listings.id]
  list_name varchar(100) [not null, default: 'Favorites', note: 'e.g. Summer 2027, Ski trip']
  saved_at timestamptz [not null, default: `now()`]

  indexes {
    (user_id, listing_id, list_name) [pk]
  }

  Note: 'Wishlists.'
}

// ---------------------------------------------------------------------------
// LISTINGS
// ---------------------------------------------------------------------------

Table listings [headercolor: #4c8bf5] {
  id uuid [pk]
  host_id uuid [not null, ref: > users.id]
  title varchar(150) [not null]
  description text [not null]
  room_type room_type [not null]
  property_type varchar(50) [not null, note: 'e.g. Apartment, Cabin, Villa, Houseboat']
  address_line1 varchar(200) [not null]
  city varchar(120) [not null]
  country char(2) [not null]
  latitude numeric(9,6) [not null, note: 'Exact point is shown only to confirmed guests; search shows a blurred area']
  longitude numeric(9,6) [not null]
  timezone varchar(64) [not null, note: 'IANA zone. Check-in times and calendar nights are local to the property.']
  max_guests smallint [not null]
  bedrooms smallint [not null]
  beds smallint [not null]
  bathrooms numeric(3,1) [not null, note: '1.5 means one full and one half bath']
  currency char(3) [not null]
  base_nightly_cents integer [not null, note: 'Default price for a night with no rule or override']
  cleaning_fee_cents integer [not null, default: 0, note: 'Charged once per stay']
  guests_included smallint [not null, default: 1]
  extra_guest_fee_cents integer [not null, default: 0, note: 'Per night, for each guest above guests_included']
  min_nights smallint [not null, default: 1]
  max_nights smallint
  check_in_from time [not null]
  check_out_by time [not null]
  booking_mode booking_mode [not null, default: 'request']
  cancellation_policy_id uuid [not null, ref: > cancellation_policies.id]
  status listing_status [not null, default: 'draft']
  created_at timestamptz [not null, default: `now()`]

  indexes {
    host_id
    (country, city, status) [note: 'Destination search; a geo index on (latitude, longitude) serves map search']
  }

  Note: 'A place to stay, owned by one host. Base price, cleaning fee and guest limits live here; adjustments live in pricing_rules and calendar_nights.'
}

Table listing_photos [headercolor: #4c8bf5] {
  id uuid [pk]
  listing_id uuid [not null, ref: > listings.id]
  storage_key varchar(300) [not null, unique, note: 'Object storage key. Image bytes never live in the database.']
  caption varchar(200)
  position smallint [not null, note: 'Display order; the first is the cover photo']
  width integer [not null]
  height integer [not null]
  created_at timestamptz [not null, default: `now()`]

  indexes {
    (listing_id, position)
  }

  Note: 'Listing photos. Dimensions are stored so the page can reserve space before images load.'
}

Table amenities [headercolor: #4c8bf5] {
  id uuid [pk]
  code varchar(40) [not null, unique, note: 'e.g. wifi, kitchen, hot_tub, ev_charger, step_free_entrance']
  name varchar(100) [not null]
  category amenity_category [not null]
  Note: 'The fixed amenity catalog. Search filters by code, so a typo by one host never splits a filter.'
}

Table listing_amenities [headercolor: #4c8bf5] {
  listing_id uuid [not null, ref: > listings.id]
  amenity_id uuid [not null, ref: > amenities.id]
  details varchar(200) [note: 'e.g. 500 Mbps, shared with one other unit']

  indexes {
    (listing_id, amenity_id) [pk]
    (amenity_id, listing_id) [note: 'Search: listings with a hot tub']
  }

  Note: 'Which amenities a listing offers.'
}

Table house_rules [headercolor: #4c8bf5] {
  id uuid [pk]
  listing_id uuid [not null, ref: > listings.id]
  rule house_rule_kind [not null]
  is_allowed boolean [not null, note: 'e.g. pets: true, smoking: false']
  details varchar(300) [note: 'e.g. Quiet hours 22:00 to 08:00, one dog under 20 kg']

  indexes {
    listing_id
  }

  Note: 'Rules the guest must accept before booking. The booking records when they were accepted.'
}

Table cancellation_policies [headercolor: #4c8bf5] {
  id uuid [pk]
  code varchar(30) [not null, note: 'e.g. flexible, moderate, strict, non_refundable']
  version smallint [not null]
  name varchar(100) [not null]
  full_refund_days_before smallint [not null, note: 'Full refund when cancelled at least this many days before check-in']
  partial_refund_days_before smallint [note: 'Partial refund window, if any']
  partial_refund_pct smallint [note: 'Share of the nightly subtotal refunded in the partial window']
  cleaning_fee_refundable boolean [not null, default: true, note: 'Refunded whenever the stay has not started']
  effective_from timestamptz [not null]

  indexes {
    (code, version) [unique]
  }

  Note: '''Versioned rows, never edited. Changing a policy inserts a new version; a booking points at the exact version it was made under, so the refund a guest was promised can always be recomputed.'''
}

// ---------------------------------------------------------------------------
// AVAILABILITY AND PRICING
// ---------------------------------------------------------------------------

Table calendar_nights [headercolor: #4cd0b0] {
  listing_id uuid [not null, ref: > listings.id]
  night date [not null, note: 'The date the guest sleeps there. Check-out day is not a night, so back-to-back stays fit.']
  status night_status [not null, default: 'available']
  booking_id uuid [ref: > bookings.id, note: 'Set while the night is held for a request or booked']
  price_override_cents integer [note: 'Host-set price for this date; beats every rule']
  min_nights_override smallint [note: 'e.g. 3-night minimum over a holiday']
  updated_at timestamptz [not null, default: `now()`]

  indexes {
    (listing_id, night) [pk]
    booking_id
  }

  Note: '''One row per listing per night, generated for the bookable window (rolled forward daily). A booking claims its nights with one conditional UPDATE: SET status = booked, booking_id = $1 WHERE listing_id = $2 AND night >= check_in AND night < check_out AND status = available. It must touch exactly as many rows as nights booked, or the transaction rolls back. A night can carry only one booking_id, so two overlapping bookings can never both commit.'''
}

Table pricing_rules [headercolor: #4cd0b0] {
  id uuid [pk]
  listing_id uuid [not null, ref: > listings.id]
  kind pricing_rule_kind [not null]
  adjustment_pct numeric(5,2) [note: 'Negative for a discount, positive for a markup']
  nightly_price_cents integer [note: 'Alternative to a percentage: a fixed nightly price, e.g. for a season']
  start_date date [note: 'Seasonal rules']
  end_date date
  weekend_nights smallint [note: 'Weekend rules: bitmask of nights, Friday and Saturday by default']
  min_nights smallint [note: 'Length-of-stay rules: 7 for weekly, 28 for monthly']
  days_before_arrival smallint [note: 'Early bird (at least) and last minute (at most) rules']
  priority smallint [not null, default: 0, note: 'Highest wins when two nightly rules overlap']
  is_active boolean [not null, default: true]

  indexes {
    (listing_id, kind)
  }

  Note: '''How a night is priced. Resolution order: calendar override, then the highest-priority seasonal or weekend rule, then base_nightly_cents. Length-of-stay, early bird and last minute rules then discount the nightly subtotal. The cleaning fee is not a rule; it is a fixed per-stay amount on the listing.'''
}

Table calendar_syncs [headercolor: #4cd0b0] {
  id uuid [pk]
  listing_id uuid [not null, ref: > listings.id]
  direction sync_direction [not null]
  feed_url varchar(500) [not null, note: 'iCal URL. Treated as a secret: anyone with it can read the calendar.']
  last_synced_at timestamptz
  last_error varchar(300)
  Note: 'iCal feeds to and from other sites. Imported busy dates become blocked nights, which is how hosts list on several platforms without double bookings.'
}

// ---------------------------------------------------------------------------
// BOOKINGS
// ---------------------------------------------------------------------------

Table bookings [headercolor: #f5b74c] {
  id uuid [pk]
  confirmation_code varchar(12) [not null, unique]
  listing_id uuid [not null, ref: > listings.id]
  guest_id uuid [not null, ref: > users.id]
  host_id uuid [not null, ref: > users.id, note: 'Copied from the listing, so a listing changing hands never rewrites past stays']
  mode booking_mode [not null, note: 'Instant books confirm at once; requests wait for the host']
  status booking_status [not null]
  check_in date [not null]
  check_out date [not null, note: 'CHECK (check_out > check_in)']
  adults smallint [not null]
  children smallint [not null, default: 0]
  infants smallint [not null, default: 0]
  pets smallint [not null, default: 0]
  currency char(3) [not null]
  guest_total_cents integer [not null, note: 'What the guest pays: nights plus guest-side charges']
  host_payout_cents integer [not null, note: 'What the host earns: nights plus host-side charges, net of the host fee']
  cancellation_policy_id uuid [not null, ref: > cancellation_policies.id, note: 'The exact policy version in force at booking']
  house_rules_accepted_at timestamptz [not null]
  request_expires_at timestamptz [note: 'Requests auto-decline after 24 hours and release their nights']
  created_at timestamptz [not null, default: `now()`]
  confirmed_at timestamptz

  indexes {
    (listing_id, check_in)
    (guest_id, check_in)
  }

  Note: 'A stay. Totals here are the sum of booking_nights and booking_charges, frozen when the guest pays.'
}

Table booking_nights [headercolor: #f5b74c] {
  booking_id uuid [not null, ref: > bookings.id]
  night date [not null]
  price_cents integer [not null, note: 'Snapshot of the resolved nightly price']
  pricing_rule_id uuid [ref: > pricing_rules.id, note: 'The rule that set it; null for base price or a calendar override']

  indexes {
    (booking_id, night) [pk]
  }

  Note: 'The price of every night, as quoted. Editing rules later never changes a confirmed stay, and "why was Saturday more?" has an answer.'
}

Table booking_charges [headercolor: #f5b74c] {
  id uuid [pk]
  booking_id uuid [not null, ref: > bookings.id]
  kind charge_kind [not null]
  payer party [not null, note: 'Guest-side lines add to the guest total; host-side lines reduce the payout']
  description varchar(200) [not null, note: 'e.g. Weekly discount 10%, Cleaning fee, Occupancy tax 6%']
  amount_cents integer [not null, note: 'Negative for discounts']
  Note: 'Every line on both receipts: cleaning fee, extra guests, pets, discounts, service fees and taxes.'
}

Table cancellations [headercolor: #f5b74c] {
  booking_id uuid [pk, ref: - bookings.id]
  cancelled_by party [not null]
  cancelled_by_user_id uuid [ref: > users.id, note: 'Null when support cancels on behalf of either side']
  reason varchar(300)
  guest_refund_cents integer [not null, note: 'Computed from the policy version on the booking, then frozen']
  host_payout_kept_cents integer [not null, note: 'What the host still earns after the refund']
  host_penalty_cents integer [not null, default: 0, note: 'Charged when the host cancels a confirmed stay']
  cancelled_at timestamptz [not null, default: `now()`]
  Note: 'The outcome of a cancellation. The money math is stored, not recomputed later, so a policy change can never alter it.'
}

// ---------------------------------------------------------------------------
// PAYMENTS AND PAYOUTS
// ---------------------------------------------------------------------------

Table payment_methods [headercolor: #b98cf0] {
  id uuid [pk]
  user_id uuid [not null, ref: > users.id]
  processor_token varchar(200) [not null, note: 'Card data stays with the payment processor; only its token is stored']
  brand varchar(20) [not null]
  last4 char(4) [not null]
  expires_on date [not null]
  Note: 'Guest cards, as processor tokens.'
}

Table guest_payments [headercolor: #b98cf0] {
  id uuid [pk]
  booking_id uuid [not null, ref: > bookings.id]
  payment_method_id uuid [not null, ref: > payment_methods.id]
  installment smallint [not null, default: 1, note: 'Pay part now, part later: 1 at booking, 2 before arrival']
  amount_cents integer [not null]
  currency char(3) [not null]
  status payment_status [not null, default: 'scheduled']
  due_at timestamptz [not null]
  processed_at timestamptz
  processor_reference varchar(100) [unique]

  indexes {
    (status, due_at) [note: 'The collection job: scheduled installments now due']
  }

  Note: 'Money in, from the guest, on the guest schedule.'
}

Table refunds [headercolor: #b98cf0] {
  id uuid [pk]
  guest_payment_id uuid [not null, ref: > guest_payments.id]
  booking_id uuid [not null, ref: > bookings.id]
  amount_cents integer [not null]
  reason refund_reason [not null]
  status payment_status [not null, default: 'scheduled']
  processor_reference varchar(100)
  created_at timestamptz [not null, default: `now()`]
  Note: 'Money back to the guest, against a specific payment, since a refund goes back to the card it came from.'
}

Table payout_accounts [headercolor: #b98cf0] {
  id uuid [pk]
  host_id uuid [not null, ref: > users.id]
  processor_account_token varchar(200) [not null, note: 'Connected-account id; bank details stay with the processor']
  currency char(3) [not null]
  country char(2) [not null]
  is_default boolean [not null, default: false]
  verified_at timestamptz
  Note: 'Where a host gets paid.'
}

Table host_payouts [headercolor: #b98cf0] {
  id uuid [pk]
  booking_id uuid [not null, ref: > bookings.id]
  host_id uuid [not null, ref: > users.id]
  payout_account_id uuid [not null, ref: > payout_accounts.id]
  amount_cents integer [not null, note: 'Negative for an adjustment, e.g. a host cancellation penalty']
  currency char(3) [not null]
  status payout_status [not null, default: 'scheduled']
  release_after timestamptz [not null, note: 'About 24 hours after check-in, once the guest has arrived']
  paid_at timestamptz
  processor_reference varchar(100) [unique]

  indexes {
    (status, release_after) [note: 'The payout job']
  }

  Note: '''Money out, to the host, on the host schedule. Guest money and host money never share a row: the platform holds funds between the guest charge and the host release, so a cancellation before arrival is a refund to the guest, never a clawback from the host.'''
}

// ---------------------------------------------------------------------------
// REVIEWS AND MESSAGING
// ---------------------------------------------------------------------------

Table reviews [headercolor: #8bc34a] {
  id uuid [pk]
  booking_id uuid [not null, ref: > bookings.id]
  direction review_direction [not null]
  author_id uuid [not null, ref: > users.id]
  subject_id uuid [not null, ref: > users.id]
  overall_rating smallint [not null, note: '1 to 5']
  cleanliness_rating smallint [note: 'Category ratings apply to guest-to-host reviews only']
  accuracy_rating smallint
  communication_rating smallint
  location_rating smallint
  value_rating smallint
  public_text text [not null]
  private_feedback text [note: 'Seen only by the other party, never published']
  submitted_at timestamptz [not null, default: `now()`]
  published_at timestamptz [not null, note: 'When it becomes visible; see the table note']

  indexes {
    (booking_id, direction) [unique]
    (subject_id, published_at)
  }

  Note: '''Two-sided and blind. When the first review of a stay arrives, its published_at is set to the deadline (check-out plus 14 days). When the second arrives, both are set to now(). Readers filter on published_at <= now(), so neither side can read the other first, and nothing has to wake up at the deadline.'''
}

Table message_threads [headercolor: #8bc34a] {
  id uuid [pk]
  listing_id uuid [not null, ref: > listings.id]
  guest_id uuid [not null, ref: > users.id]
  host_id uuid [not null, ref: > users.id]
  booking_id uuid [ref: > bookings.id, note: 'Null for an inquiry before booking']
  last_message_at timestamptz
  created_at timestamptz [not null, default: `now()`]

  indexes {
    (guest_id, last_message_at)
    (host_id, last_message_at)
  }

  Note: 'One conversation between a guest and a host about a listing, attached to the booking once there is one.'
}

Table messages [headercolor: #8bc34a] {
  id uuid [pk]
  thread_id uuid [not null, ref: > message_threads.id]
  sender_id uuid [ref: > users.id, note: 'Null for system messages, e.g. booking confirmed']
  body text [not null]
  was_redacted boolean [not null, default: false, note: 'Phone numbers and emails are masked before a booking is confirmed, to keep payment on the platform']
  sent_at timestamptz [not null, default: `now()`]
  read_at timestamptz

  indexes {
    (thread_id, sent_at)
  }

  Note: 'Messages in a thread. Kept as the record if a dispute reaches support.'
}

// ---------------------------------------------------------------------------
// ENUMS
// ---------------------------------------------------------------------------

Enum verification_kind {
  email
  phone
  government_id
}

Enum verification_status {
  pending
  verified
  failed
  expired
}

Enum room_type {
  entire_place
  private_room
  shared_room
}

Enum listing_status {
  draft
  pending_review
  listed
  unlisted
  suspended
}

Enum booking_mode {
  instant
  request
}

Enum amenity_category {
  essentials
  kitchen
  bathroom
  safety
  outdoor
  parking
  accessibility
  entertainment
}

Enum house_rule_kind {
  pets
  smoking
  parties
  children
  quiet_hours
  other
}

Enum night_status {
  available
  blocked [note: 'Host block or an imported busy date']
  held [note: 'Waiting on a booking request']
  booked
}

Enum pricing_rule_kind {
  weekend
  seasonal
  length_of_stay
  early_bird
  last_minute
}

Enum sync_direction {
  import
  export
}

Enum booking_status {
  requested
  declined
  expired
  confirmed
  cancelled
  completed
}

Enum charge_kind {
  cleaning_fee
  extra_guest_fee
  pet_fee
  discount
  service_fee
  tax
}

Enum party {
  guest
  host
  platform
}

Enum payment_status {
  scheduled
  succeeded
  failed
  cancelled
}

Enum refund_reason {
  guest_cancellation
  host_cancellation
  listing_issue
  goodwill
}

Enum payout_status {
  scheduled
  released
  paid
  failed
  reversed
}

Enum review_direction {
  guest_to_host
  host_to_guest
}

Vacation Rental Marketplace database schema as SQL

The same schema as CREATE TABLE statements, exported by DB Planner. PostgreSQL is shown first; MySQL and SQL Server are below it.

PostgreSQL
postgresql.sql PostgreSQL
CREATE TYPE "verification_kind" AS ENUM (
  'email',
  'phone',
  'government_id'
);

CREATE TYPE "verification_status" AS ENUM (
  'pending',
  'verified',
  'failed',
  'expired'
);

CREATE TYPE "room_type" AS ENUM (
  'entire_place',
  'private_room',
  'shared_room'
);

CREATE TYPE "listing_status" AS ENUM (
  'draft',
  'pending_review',
  'listed',
  'unlisted',
  'suspended'
);

CREATE TYPE "booking_mode" AS ENUM (
  'instant',
  'request'
);

CREATE TYPE "amenity_category" AS ENUM (
  'essentials',
  'kitchen',
  'bathroom',
  'safety',
  'outdoor',
  'parking',
  'accessibility',
  'entertainment'
);

CREATE TYPE "house_rule_kind" AS ENUM (
  'pets',
  'smoking',
  'parties',
  'children',
  'quiet_hours',
  'other'
);

CREATE TYPE "night_status" AS ENUM (
  'available',
  'blocked',
  'held',
  'booked'
);

CREATE TYPE "pricing_rule_kind" AS ENUM (
  'weekend',
  'seasonal',
  'length_of_stay',
  'early_bird',
  'last_minute'
);

CREATE TYPE "sync_direction" AS ENUM (
  'import',
  'export'
);

CREATE TYPE "booking_status" AS ENUM (
  'requested',
  'declined',
  'expired',
  'confirmed',
  'cancelled',
  'completed'
);

CREATE TYPE "charge_kind" AS ENUM (
  'cleaning_fee',
  'extra_guest_fee',
  'pet_fee',
  'discount',
  'service_fee',
  'tax'
);

CREATE TYPE "party" AS ENUM (
  'guest',
  'host',
  'platform'
);

CREATE TYPE "payment_status" AS ENUM (
  'scheduled',
  'succeeded',
  'failed',
  'cancelled'
);

CREATE TYPE "refund_reason" AS ENUM (
  'guest_cancellation',
  'host_cancellation',
  'listing_issue',
  'goodwill'
);

CREATE TYPE "payout_status" AS ENUM (
  'scheduled',
  'released',
  'paid',
  'failed',
  'reversed'
);

CREATE TYPE "review_direction" AS ENUM (
  'guest_to_host',
  'host_to_guest'
);

CREATE TABLE "users" (
  "id" uuid PRIMARY KEY,
  "email" varchar(254) UNIQUE NOT NULL,
  "first_name" varchar(100) NOT NULL,
  "last_name" varchar(100) NOT NULL,
  "phone" varchar(30),
  "about" text,
  "avatar_storage_key" varchar(300),
  "preferred_currency" char(3) NOT NULL DEFAULT 'USD',
  "host_since" date,
  "created_at" timestamptz NOT NULL DEFAULT (now()),
  "deleted_at" timestamptz
);

CREATE TABLE "identity_verifications" (
  "id" uuid PRIMARY KEY,
  "user_id" uuid NOT NULL,
  "kind" verification_kind NOT NULL,
  "status" verification_status NOT NULL DEFAULT 'pending',
  "provider_reference" varchar(100),
  "verified_at" timestamptz,
  "expires_at" timestamptz
);

CREATE TABLE "saved_listings" (
  "user_id" uuid NOT NULL,
  "listing_id" uuid NOT NULL,
  "list_name" varchar(100) NOT NULL DEFAULT 'Favorites',
  "saved_at" timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY ("user_id", "listing_id", "list_name")
);

CREATE TABLE "listings" (
  "id" uuid PRIMARY KEY,
  "host_id" uuid NOT NULL,
  "title" varchar(150) NOT NULL,
  "description" text NOT NULL,
  "room_type" room_type NOT NULL,
  "property_type" varchar(50) NOT NULL,
  "address_line1" varchar(200) NOT NULL,
  "city" varchar(120) NOT NULL,
  "country" char(2) NOT NULL,
  "latitude" numeric(9,6) NOT NULL,
  "longitude" numeric(9,6) NOT NULL,
  "timezone" varchar(64) NOT NULL,
  "max_guests" smallint NOT NULL,
  "bedrooms" smallint NOT NULL,
  "beds" smallint NOT NULL,
  "bathrooms" numeric(3,1) NOT NULL,
  "currency" char(3) NOT NULL,
  "base_nightly_cents" integer NOT NULL,
  "cleaning_fee_cents" integer NOT NULL DEFAULT 0,
  "guests_included" smallint NOT NULL DEFAULT 1,
  "extra_guest_fee_cents" integer NOT NULL DEFAULT 0,
  "min_nights" smallint NOT NULL DEFAULT 1,
  "max_nights" smallint,
  "check_in_from" time NOT NULL,
  "check_out_by" time NOT NULL,
  "booking_mode" booking_mode NOT NULL DEFAULT 'request',
  "cancellation_policy_id" uuid NOT NULL,
  "status" listing_status NOT NULL DEFAULT 'draft',
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "listing_photos" (
  "id" uuid PRIMARY KEY,
  "listing_id" uuid NOT NULL,
  "storage_key" varchar(300) UNIQUE NOT NULL,
  "caption" varchar(200),
  "position" smallint NOT NULL,
  "width" integer NOT NULL,
  "height" integer NOT NULL,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "amenities" (
  "id" uuid PRIMARY KEY,
  "code" varchar(40) UNIQUE NOT NULL,
  "name" varchar(100) NOT NULL,
  "category" amenity_category NOT NULL
);

CREATE TABLE "listing_amenities" (
  "listing_id" uuid NOT NULL,
  "amenity_id" uuid NOT NULL,
  "details" varchar(200),
  PRIMARY KEY ("listing_id", "amenity_id")
);

CREATE TABLE "house_rules" (
  "id" uuid PRIMARY KEY,
  "listing_id" uuid NOT NULL,
  "rule" house_rule_kind NOT NULL,
  "is_allowed" boolean NOT NULL,
  "details" varchar(300)
);

CREATE TABLE "cancellation_policies" (
  "id" uuid PRIMARY KEY,
  "code" varchar(30) NOT NULL,
  "version" smallint NOT NULL,
  "name" varchar(100) NOT NULL,
  "full_refund_days_before" smallint NOT NULL,
  "partial_refund_days_before" smallint,
  "partial_refund_pct" smallint,
  "cleaning_fee_refundable" boolean NOT NULL DEFAULT true,
  "effective_from" timestamptz NOT NULL
);

CREATE TABLE "calendar_nights" (
  "listing_id" uuid NOT NULL,
  "night" date NOT NULL,
  "status" night_status NOT NULL DEFAULT 'available',
  "booking_id" uuid,
  "price_override_cents" integer,
  "min_nights_override" smallint,
  "updated_at" timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY ("listing_id", "night")
);

CREATE TABLE "pricing_rules" (
  "id" uuid PRIMARY KEY,
  "listing_id" uuid NOT NULL,
  "kind" pricing_rule_kind NOT NULL,
  "adjustment_pct" numeric(5,2),
  "nightly_price_cents" integer,
  "start_date" date,
  "end_date" date,
  "weekend_nights" smallint,
  "min_nights" smallint,
  "days_before_arrival" smallint,
  "priority" smallint NOT NULL DEFAULT 0,
  "is_active" boolean NOT NULL DEFAULT true
);

CREATE TABLE "calendar_syncs" (
  "id" uuid PRIMARY KEY,
  "listing_id" uuid NOT NULL,
  "direction" sync_direction NOT NULL,
  "feed_url" varchar(500) NOT NULL,
  "last_synced_at" timestamptz,
  "last_error" varchar(300)
);

CREATE TABLE "bookings" (
  "id" uuid PRIMARY KEY,
  "confirmation_code" varchar(12) UNIQUE NOT NULL,
  "listing_id" uuid NOT NULL,
  "guest_id" uuid NOT NULL,
  "host_id" uuid NOT NULL,
  "mode" booking_mode NOT NULL,
  "status" booking_status NOT NULL,
  "check_in" date NOT NULL,
  "check_out" date NOT NULL,
  "adults" smallint NOT NULL,
  "children" smallint NOT NULL DEFAULT 0,
  "infants" smallint NOT NULL DEFAULT 0,
  "pets" smallint NOT NULL DEFAULT 0,
  "currency" char(3) NOT NULL,
  "guest_total_cents" integer NOT NULL,
  "host_payout_cents" integer NOT NULL,
  "cancellation_policy_id" uuid NOT NULL,
  "house_rules_accepted_at" timestamptz NOT NULL,
  "request_expires_at" timestamptz,
  "created_at" timestamptz NOT NULL DEFAULT (now()),
  "confirmed_at" timestamptz
);

CREATE TABLE "booking_nights" (
  "booking_id" uuid NOT NULL,
  "night" date NOT NULL,
  "price_cents" integer NOT NULL,
  "pricing_rule_id" uuid,
  PRIMARY KEY ("booking_id", "night")
);

CREATE TABLE "booking_charges" (
  "id" uuid PRIMARY KEY,
  "booking_id" uuid NOT NULL,
  "kind" charge_kind NOT NULL,
  "payer" party NOT NULL,
  "description" varchar(200) NOT NULL,
  "amount_cents" integer NOT NULL
);

CREATE TABLE "cancellations" (
  "booking_id" uuid PRIMARY KEY,
  "cancelled_by" party NOT NULL,
  "cancelled_by_user_id" uuid,
  "reason" varchar(300),
  "guest_refund_cents" integer NOT NULL,
  "host_payout_kept_cents" integer NOT NULL,
  "host_penalty_cents" integer NOT NULL DEFAULT 0,
  "cancelled_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "payment_methods" (
  "id" uuid PRIMARY KEY,
  "user_id" uuid NOT NULL,
  "processor_token" varchar(200) NOT NULL,
  "brand" varchar(20) NOT NULL,
  "last4" char(4) NOT NULL,
  "expires_on" date NOT NULL
);

CREATE TABLE "guest_payments" (
  "id" uuid PRIMARY KEY,
  "booking_id" uuid NOT NULL,
  "payment_method_id" uuid NOT NULL,
  "installment" smallint NOT NULL DEFAULT 1,
  "amount_cents" integer NOT NULL,
  "currency" char(3) NOT NULL,
  "status" payment_status NOT NULL DEFAULT 'scheduled',
  "due_at" timestamptz NOT NULL,
  "processed_at" timestamptz,
  "processor_reference" varchar(100) UNIQUE
);

CREATE TABLE "refunds" (
  "id" uuid PRIMARY KEY,
  "guest_payment_id" uuid NOT NULL,
  "booking_id" uuid NOT NULL,
  "amount_cents" integer NOT NULL,
  "reason" refund_reason NOT NULL,
  "status" payment_status NOT NULL DEFAULT 'scheduled',
  "processor_reference" varchar(100),
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "payout_accounts" (
  "id" uuid PRIMARY KEY,
  "host_id" uuid NOT NULL,
  "processor_account_token" varchar(200) NOT NULL,
  "currency" char(3) NOT NULL,
  "country" char(2) NOT NULL,
  "is_default" boolean NOT NULL DEFAULT false,
  "verified_at" timestamptz
);

CREATE TABLE "host_payouts" (
  "id" uuid PRIMARY KEY,
  "booking_id" uuid NOT NULL,
  "host_id" uuid NOT NULL,
  "payout_account_id" uuid NOT NULL,
  "amount_cents" integer NOT NULL,
  "currency" char(3) NOT NULL,
  "status" payout_status NOT NULL DEFAULT 'scheduled',
  "release_after" timestamptz NOT NULL,
  "paid_at" timestamptz,
  "processor_reference" varchar(100) UNIQUE
);

CREATE TABLE "reviews" (
  "id" uuid PRIMARY KEY,
  "booking_id" uuid NOT NULL,
  "direction" review_direction NOT NULL,
  "author_id" uuid NOT NULL,
  "subject_id" uuid NOT NULL,
  "overall_rating" smallint NOT NULL,
  "cleanliness_rating" smallint,
  "accuracy_rating" smallint,
  "communication_rating" smallint,
  "location_rating" smallint,
  "value_rating" smallint,
  "public_text" text NOT NULL,
  "private_feedback" text,
  "submitted_at" timestamptz NOT NULL DEFAULT (now()),
  "published_at" timestamptz NOT NULL
);

CREATE TABLE "message_threads" (
  "id" uuid PRIMARY KEY,
  "listing_id" uuid NOT NULL,
  "guest_id" uuid NOT NULL,
  "host_id" uuid NOT NULL,
  "booking_id" uuid,
  "last_message_at" timestamptz,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "messages" (
  "id" uuid PRIMARY KEY,
  "thread_id" uuid NOT NULL,
  "sender_id" uuid,
  "body" text NOT NULL,
  "was_redacted" boolean NOT NULL DEFAULT false,
  "sent_at" timestamptz NOT NULL DEFAULT (now()),
  "read_at" timestamptz
);

CREATE INDEX ON "identity_verifications" ("user_id", "kind");

CREATE INDEX ON "listings" ("host_id");

CREATE INDEX ON "listings" ("country", "city", "status");

CREATE INDEX ON "listing_photos" ("listing_id", "position");

CREATE INDEX ON "listing_amenities" ("amenity_id", "listing_id");

CREATE INDEX ON "house_rules" ("listing_id");

CREATE UNIQUE INDEX ON "cancellation_policies" ("code", "version");

CREATE INDEX ON "calendar_nights" ("booking_id");

CREATE INDEX ON "pricing_rules" ("listing_id", "kind");

CREATE INDEX ON "bookings" ("listing_id", "check_in");

CREATE INDEX ON "bookings" ("guest_id", "check_in");

CREATE INDEX ON "guest_payments" ("status", "due_at");

CREATE INDEX ON "host_payouts" ("status", "release_after");

CREATE UNIQUE INDEX ON "reviews" ("booking_id", "direction");

CREATE INDEX ON "reviews" ("subject_id", "published_at");

CREATE INDEX ON "message_threads" ("guest_id", "last_message_at");

CREATE INDEX ON "message_threads" ("host_id", "last_message_at");

CREATE INDEX ON "messages" ("thread_id", "sent_at");

COMMENT ON TABLE "users" IS 'Everyone on the marketplace. A host is simply a user with listings, and most hosts travel as guests too.';

COMMENT ON COLUMN "users"."host_since" IS 'Set when the first listing goes live. Hosts and guests are the same users.';

COMMENT ON COLUMN "users"."deleted_at" IS 'Soft delete: past bookings and reviews must still name both sides';

COMMENT ON TABLE "identity_verifications" IS 'Trust checks: email, phone, government ID. Hosts can require a verified ID before accepting a booking.';

COMMENT ON COLUMN "identity_verifications"."provider_reference" IS 'Verification vendor reference; document images stay with the vendor';

COMMENT ON TABLE "saved_listings" IS 'Wishlists.';

COMMENT ON COLUMN "saved_listings"."list_name" IS 'e.g. Summer 2027, Ski trip';

COMMENT ON TABLE "listings" IS 'A place to stay, owned by one host. Base price, cleaning fee and guest limits live here; adjustments live in pricing_rules and calendar_nights.';

COMMENT ON COLUMN "listings"."property_type" IS 'e.g. Apartment, Cabin, Villa, Houseboat';

COMMENT ON COLUMN "listings"."latitude" IS 'Exact point is shown only to confirmed guests; search shows a blurred area';

COMMENT ON COLUMN "listings"."timezone" IS 'IANA zone. Check-in times and calendar nights are local to the property.';

COMMENT ON COLUMN "listings"."bathrooms" IS '1.5 means one full and one half bath';

COMMENT ON COLUMN "listings"."base_nightly_cents" IS 'Default price for a night with no rule or override';

COMMENT ON COLUMN "listings"."cleaning_fee_cents" IS 'Charged once per stay';

COMMENT ON COLUMN "listings"."extra_guest_fee_cents" IS 'Per night, for each guest above guests_included';

COMMENT ON TABLE "listing_photos" IS 'Listing photos. Dimensions are stored so the page can reserve space before images load.';

COMMENT ON COLUMN "listing_photos"."storage_key" IS 'Object storage key. Image bytes never live in the database.';

COMMENT ON COLUMN "listing_photos"."position" IS 'Display order; the first is the cover photo';

COMMENT ON TABLE "amenities" IS 'The fixed amenity catalog. Search filters by code, so a typo by one host never splits a filter.';

COMMENT ON COLUMN "amenities"."code" IS 'e.g. wifi, kitchen, hot_tub, ev_charger, step_free_entrance';

COMMENT ON TABLE "listing_amenities" IS 'Which amenities a listing offers.';

COMMENT ON COLUMN "listing_amenities"."details" IS 'e.g. 500 Mbps, shared with one other unit';

COMMENT ON TABLE "house_rules" IS 'Rules the guest must accept before booking. The booking records when they were accepted.';

COMMENT ON COLUMN "house_rules"."is_allowed" IS 'e.g. pets: true, smoking: false';

COMMENT ON COLUMN "house_rules"."details" IS 'e.g. Quiet hours 22:00 to 08:00, one dog under 20 kg';

COMMENT ON TABLE "cancellation_policies" IS 'Versioned rows, never edited. Changing a policy inserts a new version; a booking points at the exact version it was made under, so the refund a guest was promised can always be recomputed.';

COMMENT ON COLUMN "cancellation_policies"."code" IS 'e.g. flexible, moderate, strict, non_refundable';

COMMENT ON COLUMN "cancellation_policies"."full_refund_days_before" IS 'Full refund when cancelled at least this many days before check-in';

COMMENT ON COLUMN "cancellation_policies"."partial_refund_days_before" IS 'Partial refund window, if any';

COMMENT ON COLUMN "cancellation_policies"."partial_refund_pct" IS 'Share of the nightly subtotal refunded in the partial window';

COMMENT ON COLUMN "cancellation_policies"."cleaning_fee_refundable" IS 'Refunded whenever the stay has not started';

COMMENT ON TABLE "calendar_nights" IS 'One row per listing per night, generated for the bookable window (rolled forward daily). A booking claims its nights with one conditional UPDATE: SET status = booked, booking_id = $1 WHERE listing_id = $2 AND night >= check_in AND night < check_out AND status = available. It must touch exactly as many rows as nights booked, or the transaction rolls back. A night can carry only one booking_id, so two overlapping bookings can never both commit.';

COMMENT ON COLUMN "calendar_nights"."night" IS 'The date the guest sleeps there. Check-out day is not a night, so back-to-back stays fit.';

COMMENT ON COLUMN "calendar_nights"."booking_id" IS 'Set while the night is held for a request or booked';

COMMENT ON COLUMN "calendar_nights"."price_override_cents" IS 'Host-set price for this date; beats every rule';

COMMENT ON COLUMN "calendar_nights"."min_nights_override" IS 'e.g. 3-night minimum over a holiday';

COMMENT ON TABLE "pricing_rules" IS 'How a night is priced. Resolution order: calendar override, then the highest-priority seasonal or weekend rule, then base_nightly_cents. Length-of-stay, early bird and last minute rules then discount the nightly subtotal. The cleaning fee is not a rule; it is a fixed per-stay amount on the listing.';

COMMENT ON COLUMN "pricing_rules"."adjustment_pct" IS 'Negative for a discount, positive for a markup';

COMMENT ON COLUMN "pricing_rules"."nightly_price_cents" IS 'Alternative to a percentage: a fixed nightly price, e.g. for a season';

COMMENT ON COLUMN "pricing_rules"."start_date" IS 'Seasonal rules';

COMMENT ON COLUMN "pricing_rules"."weekend_nights" IS 'Weekend rules: bitmask of nights, Friday and Saturday by default';

COMMENT ON COLUMN "pricing_rules"."min_nights" IS 'Length-of-stay rules: 7 for weekly, 28 for monthly';

COMMENT ON COLUMN "pricing_rules"."days_before_arrival" IS 'Early bird (at least) and last minute (at most) rules';

COMMENT ON COLUMN "pricing_rules"."priority" IS 'Highest wins when two nightly rules overlap';

COMMENT ON TABLE "calendar_syncs" IS 'iCal feeds to and from other sites. Imported busy dates become blocked nights, which is how hosts list on several platforms without double bookings.';

COMMENT ON COLUMN "calendar_syncs"."feed_url" IS 'iCal URL. Treated as a secret: anyone with it can read the calendar.';

COMMENT ON TABLE "bookings" IS 'A stay. Totals here are the sum of booking_nights and booking_charges, frozen when the guest pays.';

COMMENT ON COLUMN "bookings"."host_id" IS 'Copied from the listing, so a listing changing hands never rewrites past stays';

COMMENT ON COLUMN "bookings"."mode" IS 'Instant books confirm at once; requests wait for the host';

COMMENT ON COLUMN "bookings"."check_out" IS 'CHECK (check_out > check_in)';

COMMENT ON COLUMN "bookings"."guest_total_cents" IS 'What the guest pays: nights plus guest-side charges';

COMMENT ON COLUMN "bookings"."host_payout_cents" IS 'What the host earns: nights plus host-side charges, net of the host fee';

COMMENT ON COLUMN "bookings"."cancellation_policy_id" IS 'The exact policy version in force at booking';

COMMENT ON COLUMN "bookings"."request_expires_at" IS 'Requests auto-decline after 24 hours and release their nights';

COMMENT ON TABLE "booking_nights" IS 'The price of every night, as quoted. Editing rules later never changes a confirmed stay, and "why was Saturday more?" has an answer.';

COMMENT ON COLUMN "booking_nights"."price_cents" IS 'Snapshot of the resolved nightly price';

COMMENT ON COLUMN "booking_nights"."pricing_rule_id" IS 'The rule that set it; null for base price or a calendar override';

COMMENT ON TABLE "booking_charges" IS 'Every line on both receipts: cleaning fee, extra guests, pets, discounts, service fees and taxes.';

COMMENT ON COLUMN "booking_charges"."payer" IS 'Guest-side lines add to the guest total; host-side lines reduce the payout';

COMMENT ON COLUMN "booking_charges"."description" IS 'e.g. Weekly discount 10%, Cleaning fee, Occupancy tax 6%';

COMMENT ON COLUMN "booking_charges"."amount_cents" IS 'Negative for discounts';

COMMENT ON TABLE "cancellations" IS 'The outcome of a cancellation. The money math is stored, not recomputed later, so a policy change can never alter it.';

COMMENT ON COLUMN "cancellations"."cancelled_by_user_id" IS 'Null when support cancels on behalf of either side';

COMMENT ON COLUMN "cancellations"."guest_refund_cents" IS 'Computed from the policy version on the booking, then frozen';

COMMENT ON COLUMN "cancellations"."host_payout_kept_cents" IS 'What the host still earns after the refund';

COMMENT ON COLUMN "cancellations"."host_penalty_cents" IS 'Charged when the host cancels a confirmed stay';

COMMENT ON TABLE "payment_methods" IS 'Guest cards, as processor tokens.';

COMMENT ON COLUMN "payment_methods"."processor_token" IS 'Card data stays with the payment processor; only its token is stored';

COMMENT ON TABLE "guest_payments" IS 'Money in, from the guest, on the guest schedule.';

COMMENT ON COLUMN "guest_payments"."installment" IS 'Pay part now, part later: 1 at booking, 2 before arrival';

COMMENT ON TABLE "refunds" IS 'Money back to the guest, against a specific payment, since a refund goes back to the card it came from.';

COMMENT ON TABLE "payout_accounts" IS 'Where a host gets paid.';

COMMENT ON COLUMN "payout_accounts"."processor_account_token" IS 'Connected-account id; bank details stay with the processor';

COMMENT ON TABLE "host_payouts" IS 'Money out, to the host, on the host schedule. Guest money and host money never share a row: the platform holds funds between the guest charge and the host release, so a cancellation before arrival is a refund to the guest, never a clawback from the host.';

COMMENT ON COLUMN "host_payouts"."amount_cents" IS 'Negative for an adjustment, e.g. a host cancellation penalty';

COMMENT ON COLUMN "host_payouts"."release_after" IS 'About 24 hours after check-in, once the guest has arrived';

COMMENT ON TABLE "reviews" IS 'Two-sided and blind. When the first review of a stay arrives, its published_at is set to the deadline (check-out plus 14 days). When the second arrives, both are set to now(). Readers filter on published_at <= now(), so neither side can read the other first, and nothing has to wake up at the deadline.';

COMMENT ON COLUMN "reviews"."overall_rating" IS '1 to 5';

COMMENT ON COLUMN "reviews"."cleanliness_rating" IS 'Category ratings apply to guest-to-host reviews only';

COMMENT ON COLUMN "reviews"."private_feedback" IS 'Seen only by the other party, never published';

COMMENT ON COLUMN "reviews"."published_at" IS 'When it becomes visible; see the table note';

COMMENT ON TABLE "message_threads" IS 'One conversation between a guest and a host about a listing, attached to the booking once there is one.';

COMMENT ON COLUMN "message_threads"."booking_id" IS 'Null for an inquiry before booking';

COMMENT ON TABLE "messages" IS 'Messages in a thread. Kept as the record if a dispute reaches support.';

COMMENT ON COLUMN "messages"."sender_id" IS 'Null for system messages, e.g. booking confirmed';

COMMENT ON COLUMN "messages"."was_redacted" IS 'Phone numbers and emails are masked before a booking is confirmed, to keep payment on the platform';

ALTER TABLE "identity_verifications" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "saved_listings" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "saved_listings" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "listings" ADD FOREIGN KEY ("host_id") REFERENCES "users" ("id");

ALTER TABLE "listings" ADD FOREIGN KEY ("cancellation_policy_id") REFERENCES "cancellation_policies" ("id");

ALTER TABLE "listing_photos" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "listing_amenities" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "listing_amenities" ADD FOREIGN KEY ("amenity_id") REFERENCES "amenities" ("id");

ALTER TABLE "house_rules" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "calendar_nights" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "calendar_nights" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "pricing_rules" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "calendar_syncs" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "bookings" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "bookings" ADD FOREIGN KEY ("guest_id") REFERENCES "users" ("id");

ALTER TABLE "bookings" ADD FOREIGN KEY ("host_id") REFERENCES "users" ("id");

ALTER TABLE "bookings" ADD FOREIGN KEY ("cancellation_policy_id") REFERENCES "cancellation_policies" ("id");

ALTER TABLE "booking_nights" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "booking_nights" ADD FOREIGN KEY ("pricing_rule_id") REFERENCES "pricing_rules" ("id");

ALTER TABLE "booking_charges" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "cancellations" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "cancellations" ADD FOREIGN KEY ("cancelled_by_user_id") REFERENCES "users" ("id");

ALTER TABLE "payment_methods" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "guest_payments" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "guest_payments" ADD FOREIGN KEY ("payment_method_id") REFERENCES "payment_methods" ("id");

ALTER TABLE "refunds" ADD FOREIGN KEY ("guest_payment_id") REFERENCES "guest_payments" ("id");

ALTER TABLE "refunds" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "payout_accounts" ADD FOREIGN KEY ("host_id") REFERENCES "users" ("id");

ALTER TABLE "host_payouts" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "host_payouts" ADD FOREIGN KEY ("host_id") REFERENCES "users" ("id");

ALTER TABLE "host_payouts" ADD FOREIGN KEY ("payout_account_id") REFERENCES "payout_accounts" ("id");

ALTER TABLE "reviews" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "reviews" ADD FOREIGN KEY ("author_id") REFERENCES "users" ("id");

ALTER TABLE "reviews" ADD FOREIGN KEY ("subject_id") REFERENCES "users" ("id");

ALTER TABLE "message_threads" ADD FOREIGN KEY ("listing_id") REFERENCES "listings" ("id");

ALTER TABLE "message_threads" ADD FOREIGN KEY ("guest_id") REFERENCES "users" ("id");

ALTER TABLE "message_threads" ADD FOREIGN KEY ("host_id") REFERENCES "users" ("id");

ALTER TABLE "message_threads" ADD FOREIGN KEY ("booking_id") REFERENCES "bookings" ("id");

ALTER TABLE "messages" ADD FOREIGN KEY ("thread_id") REFERENCES "message_threads" ("id");

ALTER TABLE "messages" ADD FOREIGN KEY ("sender_id") REFERENCES "users" ("id");
MySQL
mysql.sql MySQL
CREATE TABLE `users` (
  `id` uuid PRIMARY KEY,
  `email` varchar(254) UNIQUE NOT NULL,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `phone` varchar(30),
  `about` text,
  `avatar_storage_key` varchar(300),
  `preferred_currency` char(3) NOT NULL DEFAULT 'USD',
  `host_since` date COMMENT 'Set when the first listing goes live. Hosts and guests are the same users.',
  `created_at` timestamptz NOT NULL DEFAULT (now()),
  `deleted_at` timestamptz COMMENT 'Soft delete: past bookings and reviews must still name both sides'
);

CREATE TABLE `identity_verifications` (
  `id` uuid PRIMARY KEY,
  `user_id` uuid NOT NULL,
  `kind` ENUM ('email', 'phone', 'government_id') NOT NULL,
  `status` ENUM ('pending', 'verified', 'failed', 'expired') NOT NULL DEFAULT 'pending',
  `provider_reference` varchar(100) COMMENT 'Verification vendor reference; document images stay with the vendor',
  `verified_at` timestamptz,
  `expires_at` timestamptz
);

CREATE TABLE `saved_listings` (
  `user_id` uuid NOT NULL,
  `listing_id` uuid NOT NULL,
  `list_name` varchar(100) NOT NULL DEFAULT 'Favorites' COMMENT 'e.g. Summer 2027, Ski trip',
  `saved_at` timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY (`user_id`, `listing_id`, `list_name`)
);

CREATE TABLE `listings` (
  `id` uuid PRIMARY KEY,
  `host_id` uuid NOT NULL,
  `title` varchar(150) NOT NULL,
  `description` text NOT NULL,
  `room_type` ENUM ('entire_place', 'private_room', 'shared_room') NOT NULL,
  `property_type` varchar(50) NOT NULL COMMENT 'e.g. Apartment, Cabin, Villa, Houseboat',
  `address_line1` varchar(200) NOT NULL,
  `city` varchar(120) NOT NULL,
  `country` char(2) NOT NULL,
  `latitude` numeric(9,6) NOT NULL COMMENT 'Exact point is shown only to confirmed guests; search shows a blurred area',
  `longitude` numeric(9,6) NOT NULL,
  `timezone` varchar(64) NOT NULL COMMENT 'IANA zone. Check-in times and calendar nights are local to the property.',
  `max_guests` smallint NOT NULL,
  `bedrooms` smallint NOT NULL,
  `beds` smallint NOT NULL,
  `bathrooms` numeric(3,1) NOT NULL COMMENT '1.5 means one full and one half bath',
  `currency` char(3) NOT NULL,
  `base_nightly_cents` integer NOT NULL COMMENT 'Default price for a night with no rule or override',
  `cleaning_fee_cents` integer NOT NULL DEFAULT 0 COMMENT 'Charged once per stay',
  `guests_included` smallint NOT NULL DEFAULT 1,
  `extra_guest_fee_cents` integer NOT NULL DEFAULT 0 COMMENT 'Per night, for each guest above guests_included',
  `min_nights` smallint NOT NULL DEFAULT 1,
  `max_nights` smallint,
  `check_in_from` time NOT NULL,
  `check_out_by` time NOT NULL,
  `booking_mode` ENUM ('instant', 'request') NOT NULL DEFAULT 'request',
  `cancellation_policy_id` uuid NOT NULL,
  `status` ENUM ('draft', 'pending_review', 'listed', 'unlisted', 'suspended') NOT NULL DEFAULT 'draft',
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `listing_photos` (
  `id` uuid PRIMARY KEY,
  `listing_id` uuid NOT NULL,
  `storage_key` varchar(300) UNIQUE NOT NULL COMMENT 'Object storage key. Image bytes never live in the database.',
  `caption` varchar(200),
  `position` smallint NOT NULL COMMENT 'Display order; the first is the cover photo',
  `width` integer NOT NULL,
  `height` integer NOT NULL,
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `amenities` (
  `id` uuid PRIMARY KEY,
  `code` varchar(40) UNIQUE NOT NULL COMMENT 'e.g. wifi, kitchen, hot_tub, ev_charger, step_free_entrance',
  `name` varchar(100) NOT NULL,
  `category` ENUM ('essentials', 'kitchen', 'bathroom', 'safety', 'outdoor', 'parking', 'accessibility', 'entertainment') NOT NULL
);

CREATE TABLE `listing_amenities` (
  `listing_id` uuid NOT NULL,
  `amenity_id` uuid NOT NULL,
  `details` varchar(200) COMMENT 'e.g. 500 Mbps, shared with one other unit',
  PRIMARY KEY (`listing_id`, `amenity_id`)
);

CREATE TABLE `house_rules` (
  `id` uuid PRIMARY KEY,
  `listing_id` uuid NOT NULL,
  `rule` ENUM ('pets', 'smoking', 'parties', 'children', 'quiet_hours', 'other') NOT NULL,
  `is_allowed` boolean NOT NULL COMMENT 'e.g. pets: true, smoking: false',
  `details` varchar(300) COMMENT 'e.g. Quiet hours 22:00 to 08:00, one dog under 20 kg'
);

CREATE TABLE `cancellation_policies` (
  `id` uuid PRIMARY KEY,
  `code` varchar(30) NOT NULL COMMENT 'e.g. flexible, moderate, strict, non_refundable',
  `version` smallint NOT NULL,
  `name` varchar(100) NOT NULL,
  `full_refund_days_before` smallint NOT NULL COMMENT 'Full refund when cancelled at least this many days before check-in',
  `partial_refund_days_before` smallint COMMENT 'Partial refund window, if any',
  `partial_refund_pct` smallint COMMENT 'Share of the nightly subtotal refunded in the partial window',
  `cleaning_fee_refundable` boolean NOT NULL DEFAULT true COMMENT 'Refunded whenever the stay has not started',
  `effective_from` timestamptz NOT NULL
);

CREATE TABLE `calendar_nights` (
  `listing_id` uuid NOT NULL,
  `night` date NOT NULL COMMENT 'The date the guest sleeps there. Check-out day is not a night, so back-to-back stays fit.',
  `status` ENUM ('available', 'blocked', 'held', 'booked') NOT NULL DEFAULT 'available',
  `booking_id` uuid COMMENT 'Set while the night is held for a request or booked',
  `price_override_cents` integer COMMENT 'Host-set price for this date; beats every rule',
  `min_nights_override` smallint COMMENT 'e.g. 3-night minimum over a holiday',
  `updated_at` timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY (`listing_id`, `night`)
);

CREATE TABLE `pricing_rules` (
  `id` uuid PRIMARY KEY,
  `listing_id` uuid NOT NULL,
  `kind` ENUM ('weekend', 'seasonal', 'length_of_stay', 'early_bird', 'last_minute') NOT NULL,
  `adjustment_pct` numeric(5,2) COMMENT 'Negative for a discount, positive for a markup',
  `nightly_price_cents` integer COMMENT 'Alternative to a percentage: a fixed nightly price, e.g. for a season',
  `start_date` date COMMENT 'Seasonal rules',
  `end_date` date,
  `weekend_nights` smallint COMMENT 'Weekend rules: bitmask of nights, Friday and Saturday by default',
  `min_nights` smallint COMMENT 'Length-of-stay rules: 7 for weekly, 28 for monthly',
  `days_before_arrival` smallint COMMENT 'Early bird (at least) and last minute (at most) rules',
  `priority` smallint NOT NULL DEFAULT 0 COMMENT 'Highest wins when two nightly rules overlap',
  `is_active` boolean NOT NULL DEFAULT true
);

CREATE TABLE `calendar_syncs` (
  `id` uuid PRIMARY KEY,
  `listing_id` uuid NOT NULL,
  `direction` ENUM ('import', 'export') NOT NULL,
  `feed_url` varchar(500) NOT NULL COMMENT 'iCal URL. Treated as a secret: anyone with it can read the calendar.',
  `last_synced_at` timestamptz,
  `last_error` varchar(300)
);

CREATE TABLE `bookings` (
  `id` uuid PRIMARY KEY,
  `confirmation_code` varchar(12) UNIQUE NOT NULL,
  `listing_id` uuid NOT NULL,
  `guest_id` uuid NOT NULL,
  `host_id` uuid NOT NULL COMMENT 'Copied from the listing, so a listing changing hands never rewrites past stays',
  `mode` ENUM ('instant', 'request') NOT NULL COMMENT 'Instant books confirm at once; requests wait for the host',
  `status` ENUM ('requested', 'declined', 'expired', 'confirmed', 'cancelled', 'completed') NOT NULL,
  `check_in` date NOT NULL,
  `check_out` date NOT NULL COMMENT 'CHECK (check_out > check_in)',
  `adults` smallint NOT NULL,
  `children` smallint NOT NULL DEFAULT 0,
  `infants` smallint NOT NULL DEFAULT 0,
  `pets` smallint NOT NULL DEFAULT 0,
  `currency` char(3) NOT NULL,
  `guest_total_cents` integer NOT NULL COMMENT 'What the guest pays: nights plus guest-side charges',
  `host_payout_cents` integer NOT NULL COMMENT 'What the host earns: nights plus host-side charges, net of the host fee',
  `cancellation_policy_id` uuid NOT NULL COMMENT 'The exact policy version in force at booking',
  `house_rules_accepted_at` timestamptz NOT NULL,
  `request_expires_at` timestamptz COMMENT 'Requests auto-decline after 24 hours and release their nights',
  `created_at` timestamptz NOT NULL DEFAULT (now()),
  `confirmed_at` timestamptz
);

CREATE TABLE `booking_nights` (
  `booking_id` uuid NOT NULL,
  `night` date NOT NULL,
  `price_cents` integer NOT NULL COMMENT 'Snapshot of the resolved nightly price',
  `pricing_rule_id` uuid COMMENT 'The rule that set it; null for base price or a calendar override',
  PRIMARY KEY (`booking_id`, `night`)
);

CREATE TABLE `booking_charges` (
  `id` uuid PRIMARY KEY,
  `booking_id` uuid NOT NULL,
  `kind` ENUM ('cleaning_fee', 'extra_guest_fee', 'pet_fee', 'discount', 'service_fee', 'tax') NOT NULL,
  `payer` ENUM ('guest', 'host', 'platform') NOT NULL COMMENT 'Guest-side lines add to the guest total; host-side lines reduce the payout',
  `description` varchar(200) NOT NULL COMMENT 'e.g. Weekly discount 10%, Cleaning fee, Occupancy tax 6%',
  `amount_cents` integer NOT NULL COMMENT 'Negative for discounts'
);

CREATE TABLE `cancellations` (
  `booking_id` uuid PRIMARY KEY,
  `cancelled_by` ENUM ('guest', 'host', 'platform') NOT NULL,
  `cancelled_by_user_id` uuid COMMENT 'Null when support cancels on behalf of either side',
  `reason` varchar(300),
  `guest_refund_cents` integer NOT NULL COMMENT 'Computed from the policy version on the booking, then frozen',
  `host_payout_kept_cents` integer NOT NULL COMMENT 'What the host still earns after the refund',
  `host_penalty_cents` integer NOT NULL DEFAULT 0 COMMENT 'Charged when the host cancels a confirmed stay',
  `cancelled_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `payment_methods` (
  `id` uuid PRIMARY KEY,
  `user_id` uuid NOT NULL,
  `processor_token` varchar(200) NOT NULL COMMENT 'Card data stays with the payment processor; only its token is stored',
  `brand` varchar(20) NOT NULL,
  `last4` char(4) NOT NULL,
  `expires_on` date NOT NULL
);

CREATE TABLE `guest_payments` (
  `id` uuid PRIMARY KEY,
  `booking_id` uuid NOT NULL,
  `payment_method_id` uuid NOT NULL,
  `installment` smallint NOT NULL DEFAULT 1 COMMENT 'Pay part now, part later: 1 at booking, 2 before arrival',
  `amount_cents` integer NOT NULL,
  `currency` char(3) NOT NULL,
  `status` ENUM ('scheduled', 'succeeded', 'failed', 'cancelled') NOT NULL DEFAULT 'scheduled',
  `due_at` timestamptz NOT NULL,
  `processed_at` timestamptz,
  `processor_reference` varchar(100) UNIQUE
);

CREATE TABLE `refunds` (
  `id` uuid PRIMARY KEY,
  `guest_payment_id` uuid NOT NULL,
  `booking_id` uuid NOT NULL,
  `amount_cents` integer NOT NULL,
  `reason` ENUM ('guest_cancellation', 'host_cancellation', 'listing_issue', 'goodwill') NOT NULL,
  `status` ENUM ('scheduled', 'succeeded', 'failed', 'cancelled') NOT NULL DEFAULT 'scheduled',
  `processor_reference` varchar(100),
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `payout_accounts` (
  `id` uuid PRIMARY KEY,
  `host_id` uuid NOT NULL,
  `processor_account_token` varchar(200) NOT NULL COMMENT 'Connected-account id; bank details stay with the processor',
  `currency` char(3) NOT NULL,
  `country` char(2) NOT NULL,
  `is_default` boolean NOT NULL DEFAULT false,
  `verified_at` timestamptz
);

CREATE TABLE `host_payouts` (
  `id` uuid PRIMARY KEY,
  `booking_id` uuid NOT NULL,
  `host_id` uuid NOT NULL,
  `payout_account_id` uuid NOT NULL,
  `amount_cents` integer NOT NULL COMMENT 'Negative for an adjustment, e.g. a host cancellation penalty',
  `currency` char(3) NOT NULL,
  `status` ENUM ('scheduled', 'released', 'paid', 'failed', 'reversed') NOT NULL DEFAULT 'scheduled',
  `release_after` timestamptz NOT NULL COMMENT 'About 24 hours after check-in, once the guest has arrived',
  `paid_at` timestamptz,
  `processor_reference` varchar(100) UNIQUE
);

CREATE TABLE `reviews` (
  `id` uuid PRIMARY KEY,
  `booking_id` uuid NOT NULL,
  `direction` ENUM ('guest_to_host', 'host_to_guest') NOT NULL,
  `author_id` uuid NOT NULL,
  `subject_id` uuid NOT NULL,
  `overall_rating` smallint NOT NULL COMMENT '1 to 5',
  `cleanliness_rating` smallint COMMENT 'Category ratings apply to guest-to-host reviews only',
  `accuracy_rating` smallint,
  `communication_rating` smallint,
  `location_rating` smallint,
  `value_rating` smallint,
  `public_text` text NOT NULL,
  `private_feedback` text COMMENT 'Seen only by the other party, never published',
  `submitted_at` timestamptz NOT NULL DEFAULT (now()),
  `published_at` timestamptz NOT NULL COMMENT 'When it becomes visible; see the table note'
);

CREATE TABLE `message_threads` (
  `id` uuid PRIMARY KEY,
  `listing_id` uuid NOT NULL,
  `guest_id` uuid NOT NULL,
  `host_id` uuid NOT NULL,
  `booking_id` uuid COMMENT 'Null for an inquiry before booking',
  `last_message_at` timestamptz,
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `messages` (
  `id` uuid PRIMARY KEY,
  `thread_id` uuid NOT NULL,
  `sender_id` uuid COMMENT 'Null for system messages, e.g. booking confirmed',
  `body` text NOT NULL,
  `was_redacted` boolean NOT NULL DEFAULT false COMMENT 'Phone numbers and emails are masked before a booking is confirmed, to keep payment on the platform',
  `sent_at` timestamptz NOT NULL DEFAULT (now()),
  `read_at` timestamptz
);

CREATE INDEX `identity_verifications_index_0` ON `identity_verifications` (`user_id`, `kind`);

CREATE INDEX `listings_index_1` ON `listings` (`host_id`);

CREATE INDEX `listings_index_2` ON `listings` (`country`, `city`, `status`);

CREATE INDEX `listing_photos_index_3` ON `listing_photos` (`listing_id`, `position`);

CREATE INDEX `listing_amenities_index_4` ON `listing_amenities` (`amenity_id`, `listing_id`);

CREATE INDEX `house_rules_index_5` ON `house_rules` (`listing_id`);

CREATE UNIQUE INDEX `cancellation_policies_index_6` ON `cancellation_policies` (`code`, `version`);

CREATE INDEX `calendar_nights_index_7` ON `calendar_nights` (`booking_id`);

CREATE INDEX `pricing_rules_index_8` ON `pricing_rules` (`listing_id`, `kind`);

CREATE INDEX `bookings_index_9` ON `bookings` (`listing_id`, `check_in`);

CREATE INDEX `bookings_index_10` ON `bookings` (`guest_id`, `check_in`);

CREATE INDEX `guest_payments_index_11` ON `guest_payments` (`status`, `due_at`);

CREATE INDEX `host_payouts_index_12` ON `host_payouts` (`status`, `release_after`);

CREATE UNIQUE INDEX `reviews_index_13` ON `reviews` (`booking_id`, `direction`);

CREATE INDEX `reviews_index_14` ON `reviews` (`subject_id`, `published_at`);

CREATE INDEX `message_threads_index_15` ON `message_threads` (`guest_id`, `last_message_at`);

CREATE INDEX `message_threads_index_16` ON `message_threads` (`host_id`, `last_message_at`);

CREATE INDEX `messages_index_17` ON `messages` (`thread_id`, `sent_at`);

ALTER TABLE `users` COMMENT = 'Everyone on the marketplace. A host is simply a user with listings, and most hosts travel as guests too.';

ALTER TABLE `identity_verifications` COMMENT = 'Trust checks: email, phone, government ID. Hosts can require a verified ID before accepting a booking.';

ALTER TABLE `saved_listings` COMMENT = 'Wishlists.';

ALTER TABLE `listings` COMMENT = 'A place to stay, owned by one host. Base price, cleaning fee and guest limits live here; adjustments live in pricing_rules and calendar_nights.';

ALTER TABLE `listing_photos` COMMENT = 'Listing photos. Dimensions are stored so the page can reserve space before images load.';

ALTER TABLE `amenities` COMMENT = 'The fixed amenity catalog. Search filters by code, so a typo by one host never splits a filter.';

ALTER TABLE `listing_amenities` COMMENT = 'Which amenities a listing offers.';

ALTER TABLE `house_rules` COMMENT = 'Rules the guest must accept before booking. The booking records when they were accepted.';

ALTER TABLE `cancellation_policies` COMMENT = 'Versioned rows, never edited. Changing a policy inserts a new version; a booking points at the exact version it was made under, so the refund a guest was promised can always be recomputed.';

ALTER TABLE `calendar_nights` COMMENT = 'One row per listing per night, generated for the bookable window (rolled forward daily). A booking claims its nights with one conditional UPDATE: SET status = booked, booking_id = $1 WHERE listing_id = $2 AND night >= check_in AND night < check_out AND status = available. It must touch exactly as many rows as nights booked, or the transaction rolls back. A night can carry only one booking_id, so two overlapping bookings can never both commit.';

ALTER TABLE `pricing_rules` COMMENT = 'How a night is priced. Resolution order: calendar override, then the highest-priority seasonal or weekend rule, then base_nightly_cents. Length-of-stay, early bird and last minute rules then discount the nightly subtotal. The cleaning fee is not a rule; it is a fixed per-stay amount on the listing.';

ALTER TABLE `calendar_syncs` COMMENT = 'iCal feeds to and from other sites. Imported busy dates become blocked nights, which is how hosts list on several platforms without double bookings.';

ALTER TABLE `bookings` COMMENT = 'A stay. Totals here are the sum of booking_nights and booking_charges, frozen when the guest pays.';

ALTER TABLE `booking_nights` COMMENT = 'The price of every night, as quoted. Editing rules later never changes a confirmed stay, and "why was Saturday more?" has an answer.';

ALTER TABLE `booking_charges` COMMENT = 'Every line on both receipts: cleaning fee, extra guests, pets, discounts, service fees and taxes.';

ALTER TABLE `cancellations` COMMENT = 'The outcome of a cancellation. The money math is stored, not recomputed later, so a policy change can never alter it.';

ALTER TABLE `payment_methods` COMMENT = 'Guest cards, as processor tokens.';

ALTER TABLE `guest_payments` COMMENT = 'Money in, from the guest, on the guest schedule.';

ALTER TABLE `refunds` COMMENT = 'Money back to the guest, against a specific payment, since a refund goes back to the card it came from.';

ALTER TABLE `payout_accounts` COMMENT = 'Where a host gets paid.';

ALTER TABLE `host_payouts` COMMENT = 'Money out, to the host, on the host schedule. Guest money and host money never share a row: the platform holds funds between the guest charge and the host release, so a cancellation before arrival is a refund to the guest, never a clawback from the host.';

ALTER TABLE `reviews` COMMENT = 'Two-sided and blind. When the first review of a stay arrives, its published_at is set to the deadline (check-out plus 14 days). When the second arrives, both are set to now(). Readers filter on published_at <= now(), so neither side can read the other first, and nothing has to wake up at the deadline.';

ALTER TABLE `message_threads` COMMENT = 'One conversation between a guest and a host about a listing, attached to the booking once there is one.';

ALTER TABLE `messages` COMMENT = 'Messages in a thread. Kept as the record if a dispute reaches support.';

ALTER TABLE `identity_verifications` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

ALTER TABLE `saved_listings` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

ALTER TABLE `saved_listings` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `listings` ADD FOREIGN KEY (`host_id`) REFERENCES `users` (`id`);

ALTER TABLE `listings` ADD FOREIGN KEY (`cancellation_policy_id`) REFERENCES `cancellation_policies` (`id`);

ALTER TABLE `listing_photos` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `listing_amenities` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `listing_amenities` ADD FOREIGN KEY (`amenity_id`) REFERENCES `amenities` (`id`);

ALTER TABLE `house_rules` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `calendar_nights` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `calendar_nights` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `pricing_rules` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `calendar_syncs` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `bookings` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `bookings` ADD FOREIGN KEY (`guest_id`) REFERENCES `users` (`id`);

ALTER TABLE `bookings` ADD FOREIGN KEY (`host_id`) REFERENCES `users` (`id`);

ALTER TABLE `bookings` ADD FOREIGN KEY (`cancellation_policy_id`) REFERENCES `cancellation_policies` (`id`);

ALTER TABLE `booking_nights` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `booking_nights` ADD FOREIGN KEY (`pricing_rule_id`) REFERENCES `pricing_rules` (`id`);

ALTER TABLE `booking_charges` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `cancellations` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `cancellations` ADD FOREIGN KEY (`cancelled_by_user_id`) REFERENCES `users` (`id`);

ALTER TABLE `payment_methods` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

ALTER TABLE `guest_payments` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `guest_payments` ADD FOREIGN KEY (`payment_method_id`) REFERENCES `payment_methods` (`id`);

ALTER TABLE `refunds` ADD FOREIGN KEY (`guest_payment_id`) REFERENCES `guest_payments` (`id`);

ALTER TABLE `refunds` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `payout_accounts` ADD FOREIGN KEY (`host_id`) REFERENCES `users` (`id`);

ALTER TABLE `host_payouts` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `host_payouts` ADD FOREIGN KEY (`host_id`) REFERENCES `users` (`id`);

ALTER TABLE `host_payouts` ADD FOREIGN KEY (`payout_account_id`) REFERENCES `payout_accounts` (`id`);

ALTER TABLE `reviews` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `reviews` ADD FOREIGN KEY (`author_id`) REFERENCES `users` (`id`);

ALTER TABLE `reviews` ADD FOREIGN KEY (`subject_id`) REFERENCES `users` (`id`);

ALTER TABLE `message_threads` ADD FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`);

ALTER TABLE `message_threads` ADD FOREIGN KEY (`guest_id`) REFERENCES `users` (`id`);

ALTER TABLE `message_threads` ADD FOREIGN KEY (`host_id`) REFERENCES `users` (`id`);

ALTER TABLE `message_threads` ADD FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`);

ALTER TABLE `messages` ADD FOREIGN KEY (`thread_id`) REFERENCES `message_threads` (`id`);

ALTER TABLE `messages` ADD FOREIGN KEY (`sender_id`) REFERENCES `users` (`id`);
SQL Server
sql-server.sql SQL Server
CREATE TABLE [users] (
  [id] uuid PRIMARY KEY,
  [email] varchar(254) UNIQUE NOT NULL,
  [first_name] varchar(100) NOT NULL,
  [last_name] varchar(100) NOT NULL,
  [phone] varchar(30),
  [about] text,
  [avatar_storage_key] varchar(300),
  [preferred_currency] char(3) NOT NULL DEFAULT 'USD',
  [host_since] date,
  [created_at] timestamptz NOT NULL DEFAULT (now()),
  [deleted_at] timestamptz
)
GO

CREATE TABLE [identity_verifications] (
  [id] uuid PRIMARY KEY,
  [user_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('email', 'phone', 'government_id')),
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('pending', 'verified', 'failed', 'expired')) DEFAULT 'pending',
  [provider_reference] varchar(100),
  [verified_at] timestamptz,
  [expires_at] timestamptz
)
GO

CREATE TABLE [saved_listings] (
  [user_id] uuid NOT NULL,
  [listing_id] uuid NOT NULL,
  [list_name] varchar(100) NOT NULL DEFAULT 'Favorites',
  [saved_at] timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY ([user_id], [listing_id], [list_name])
)
GO

CREATE TABLE [listings] (
  [id] uuid PRIMARY KEY,
  [host_id] uuid NOT NULL,
  [title] varchar(150) NOT NULL,
  [description] text NOT NULL,
  [room_type] nvarchar(255) NOT NULL CHECK ([room_type] IN ('entire_place', 'private_room', 'shared_room')),
  [property_type] varchar(50) NOT NULL,
  [address_line1] varchar(200) NOT NULL,
  [city] varchar(120) NOT NULL,
  [country] char(2) NOT NULL,
  [latitude] numeric(9,6) NOT NULL,
  [longitude] numeric(9,6) NOT NULL,
  [timezone] varchar(64) NOT NULL,
  [max_guests] smallint NOT NULL,
  [bedrooms] smallint NOT NULL,
  [beds] smallint NOT NULL,
  [bathrooms] numeric(3,1) NOT NULL,
  [currency] char(3) NOT NULL,
  [base_nightly_cents] integer NOT NULL,
  [cleaning_fee_cents] integer NOT NULL DEFAULT (0),
  [guests_included] smallint NOT NULL DEFAULT (1),
  [extra_guest_fee_cents] integer NOT NULL DEFAULT (0),
  [min_nights] smallint NOT NULL DEFAULT (1),
  [max_nights] smallint,
  [check_in_from] time NOT NULL,
  [check_out_by] time NOT NULL,
  [booking_mode] nvarchar(255) NOT NULL CHECK ([booking_mode] IN ('instant', 'request')) DEFAULT 'request',
  [cancellation_policy_id] uuid NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('draft', 'pending_review', 'listed', 'unlisted', 'suspended')) DEFAULT 'draft',
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [listing_photos] (
  [id] uuid PRIMARY KEY,
  [listing_id] uuid NOT NULL,
  [storage_key] varchar(300) UNIQUE NOT NULL,
  [caption] varchar(200),
  [position] smallint NOT NULL,
  [width] integer NOT NULL,
  [height] integer NOT NULL,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [amenities] (
  [id] uuid PRIMARY KEY,
  [code] varchar(40) UNIQUE NOT NULL,
  [name] varchar(100) NOT NULL,
  [category] nvarchar(255) NOT NULL CHECK ([category] IN ('essentials', 'kitchen', 'bathroom', 'safety', 'outdoor', 'parking', 'accessibility', 'entertainment'))
)
GO

CREATE TABLE [listing_amenities] (
  [listing_id] uuid NOT NULL,
  [amenity_id] uuid NOT NULL,
  [details] varchar(200),
  PRIMARY KEY ([listing_id], [amenity_id])
)
GO

CREATE TABLE [house_rules] (
  [id] uuid PRIMARY KEY,
  [listing_id] uuid NOT NULL,
  [rule] nvarchar(255) NOT NULL CHECK ([rule] IN ('pets', 'smoking', 'parties', 'children', 'quiet_hours', 'other')),
  [is_allowed] boolean NOT NULL,
  [details] varchar(300)
)
GO

CREATE TABLE [cancellation_policies] (
  [id] uuid PRIMARY KEY,
  [code] varchar(30) NOT NULL,
  [version] smallint NOT NULL,
  [name] varchar(100) NOT NULL,
  [full_refund_days_before] smallint NOT NULL,
  [partial_refund_days_before] smallint,
  [partial_refund_pct] smallint,
  [cleaning_fee_refundable] boolean NOT NULL DEFAULT (true),
  [effective_from] timestamptz NOT NULL
)
GO

CREATE TABLE [calendar_nights] (
  [listing_id] uuid NOT NULL,
  [night] date NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('available', 'blocked', 'held', 'booked')) DEFAULT 'available',
  [booking_id] uuid,
  [price_override_cents] integer,
  [min_nights_override] smallint,
  [updated_at] timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY ([listing_id], [night])
)
GO

CREATE TABLE [pricing_rules] (
  [id] uuid PRIMARY KEY,
  [listing_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('weekend', 'seasonal', 'length_of_stay', 'early_bird', 'last_minute')),
  [adjustment_pct] numeric(5,2),
  [nightly_price_cents] integer,
  [start_date] date,
  [end_date] date,
  [weekend_nights] smallint,
  [min_nights] smallint,
  [days_before_arrival] smallint,
  [priority] smallint NOT NULL DEFAULT (0),
  [is_active] boolean NOT NULL DEFAULT (true)
)
GO

CREATE TABLE [calendar_syncs] (
  [id] uuid PRIMARY KEY,
  [listing_id] uuid NOT NULL,
  [direction] nvarchar(255) NOT NULL CHECK ([direction] IN ('import', 'export')),
  [feed_url] varchar(500) NOT NULL,
  [last_synced_at] timestamptz,
  [last_error] varchar(300)
)
GO

CREATE TABLE [bookings] (
  [id] uuid PRIMARY KEY,
  [confirmation_code] varchar(12) UNIQUE NOT NULL,
  [listing_id] uuid NOT NULL,
  [guest_id] uuid NOT NULL,
  [host_id] uuid NOT NULL,
  [mode] nvarchar(255) NOT NULL CHECK ([mode] IN ('instant', 'request')),
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('requested', 'declined', 'expired', 'confirmed', 'cancelled', 'completed')),
  [check_in] date NOT NULL,
  [check_out] date NOT NULL,
  [adults] smallint NOT NULL,
  [children] smallint NOT NULL DEFAULT (0),
  [infants] smallint NOT NULL DEFAULT (0),
  [pets] smallint NOT NULL DEFAULT (0),
  [currency] char(3) NOT NULL,
  [guest_total_cents] integer NOT NULL,
  [host_payout_cents] integer NOT NULL,
  [cancellation_policy_id] uuid NOT NULL,
  [house_rules_accepted_at] timestamptz NOT NULL,
  [request_expires_at] timestamptz,
  [created_at] timestamptz NOT NULL DEFAULT (now()),
  [confirmed_at] timestamptz
)
GO

CREATE TABLE [booking_nights] (
  [booking_id] uuid NOT NULL,
  [night] date NOT NULL,
  [price_cents] integer NOT NULL,
  [pricing_rule_id] uuid,
  PRIMARY KEY ([booking_id], [night])
)
GO

CREATE TABLE [booking_charges] (
  [id] uuid PRIMARY KEY,
  [booking_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('cleaning_fee', 'extra_guest_fee', 'pet_fee', 'discount', 'service_fee', 'tax')),
  [payer] nvarchar(255) NOT NULL CHECK ([payer] IN ('guest', 'host', 'platform')),
  [description] varchar(200) NOT NULL,
  [amount_cents] integer NOT NULL
)
GO

CREATE TABLE [cancellations] (
  [booking_id] uuid PRIMARY KEY,
  [cancelled_by] nvarchar(255) NOT NULL CHECK ([cancelled_by] IN ('guest', 'host', 'platform')),
  [cancelled_by_user_id] uuid,
  [reason] varchar(300),
  [guest_refund_cents] integer NOT NULL,
  [host_payout_kept_cents] integer NOT NULL,
  [host_penalty_cents] integer NOT NULL DEFAULT (0),
  [cancelled_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [payment_methods] (
  [id] uuid PRIMARY KEY,
  [user_id] uuid NOT NULL,
  [processor_token] varchar(200) NOT NULL,
  [brand] varchar(20) NOT NULL,
  [last4] char(4) NOT NULL,
  [expires_on] date NOT NULL
)
GO

CREATE TABLE [guest_payments] (
  [id] uuid PRIMARY KEY,
  [booking_id] uuid NOT NULL,
  [payment_method_id] uuid NOT NULL,
  [installment] smallint NOT NULL DEFAULT (1),
  [amount_cents] integer NOT NULL,
  [currency] char(3) NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('scheduled', 'succeeded', 'failed', 'cancelled')) DEFAULT 'scheduled',
  [due_at] timestamptz NOT NULL,
  [processed_at] timestamptz,
  [processor_reference] varchar(100) UNIQUE
)
GO

CREATE TABLE [refunds] (
  [id] uuid PRIMARY KEY,
  [guest_payment_id] uuid NOT NULL,
  [booking_id] uuid NOT NULL,
  [amount_cents] integer NOT NULL,
  [reason] nvarchar(255) NOT NULL CHECK ([reason] IN ('guest_cancellation', 'host_cancellation', 'listing_issue', 'goodwill')),
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('scheduled', 'succeeded', 'failed', 'cancelled')) DEFAULT 'scheduled',
  [processor_reference] varchar(100),
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [payout_accounts] (
  [id] uuid PRIMARY KEY,
  [host_id] uuid NOT NULL,
  [processor_account_token] varchar(200) NOT NULL,
  [currency] char(3) NOT NULL,
  [country] char(2) NOT NULL,
  [is_default] boolean NOT NULL DEFAULT (false),
  [verified_at] timestamptz
)
GO

CREATE TABLE [host_payouts] (
  [id] uuid PRIMARY KEY,
  [booking_id] uuid NOT NULL,
  [host_id] uuid NOT NULL,
  [payout_account_id] uuid NOT NULL,
  [amount_cents] integer NOT NULL,
  [currency] char(3) NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('scheduled', 'released', 'paid', 'failed', 'reversed')) DEFAULT 'scheduled',
  [release_after] timestamptz NOT NULL,
  [paid_at] timestamptz,
  [processor_reference] varchar(100) UNIQUE
)
GO

CREATE TABLE [reviews] (
  [id] uuid PRIMARY KEY,
  [booking_id] uuid NOT NULL,
  [direction] nvarchar(255) NOT NULL CHECK ([direction] IN ('guest_to_host', 'host_to_guest')),
  [author_id] uuid NOT NULL,
  [subject_id] uuid NOT NULL,
  [overall_rating] smallint NOT NULL,
  [cleanliness_rating] smallint,
  [accuracy_rating] smallint,
  [communication_rating] smallint,
  [location_rating] smallint,
  [value_rating] smallint,
  [public_text] text NOT NULL,
  [private_feedback] text,
  [submitted_at] timestamptz NOT NULL DEFAULT (now()),
  [published_at] timestamptz NOT NULL
)
GO

CREATE TABLE [message_threads] (
  [id] uuid PRIMARY KEY,
  [listing_id] uuid NOT NULL,
  [guest_id] uuid NOT NULL,
  [host_id] uuid NOT NULL,
  [booking_id] uuid,
  [last_message_at] timestamptz,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [messages] (
  [id] uuid PRIMARY KEY,
  [thread_id] uuid NOT NULL,
  [sender_id] uuid,
  [body] text NOT NULL,
  [was_redacted] boolean NOT NULL DEFAULT (false),
  [sent_at] timestamptz NOT NULL DEFAULT (now()),
  [read_at] timestamptz
)
GO

CREATE INDEX [identity_verifications_index_0] ON [identity_verifications] ("user_id", "kind")
GO

CREATE INDEX [listings_index_1] ON [listings] ("host_id")
GO

CREATE INDEX [listings_index_2] ON [listings] ("country", "city", "status")
GO

CREATE INDEX [listing_photos_index_3] ON [listing_photos] ("listing_id", "position")
GO

CREATE INDEX [listing_amenities_index_4] ON [listing_amenities] ("amenity_id", "listing_id")
GO

CREATE INDEX [house_rules_index_5] ON [house_rules] ("listing_id")
GO

CREATE UNIQUE INDEX [cancellation_policies_index_6] ON [cancellation_policies] ("code", "version")
GO

CREATE INDEX [calendar_nights_index_7] ON [calendar_nights] ("booking_id")
GO

CREATE INDEX [pricing_rules_index_8] ON [pricing_rules] ("listing_id", "kind")
GO

CREATE INDEX [bookings_index_9] ON [bookings] ("listing_id", "check_in")
GO

CREATE INDEX [bookings_index_10] ON [bookings] ("guest_id", "check_in")
GO

CREATE INDEX [guest_payments_index_11] ON [guest_payments] ("status", "due_at")
GO

CREATE INDEX [host_payouts_index_12] ON [host_payouts] ("status", "release_after")
GO

CREATE UNIQUE INDEX [reviews_index_13] ON [reviews] ("booking_id", "direction")
GO

CREATE INDEX [reviews_index_14] ON [reviews] ("subject_id", "published_at")
GO

CREATE INDEX [message_threads_index_15] ON [message_threads] ("guest_id", "last_message_at")
GO

CREATE INDEX [message_threads_index_16] ON [message_threads] ("host_id", "last_message_at")
GO

CREATE INDEX [messages_index_17] ON [messages] ("thread_id", "sent_at")
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Everyone on the marketplace. A host is simply a user with listings, and most hosts travel as guests too.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'users';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set when the first listing goes live. Hosts and guests are the same users.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'users',
@level2type = N'Column', @level2name = 'host_since';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Soft delete: past bookings and reviews must still name both sides',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'users',
@level2type = N'Column', @level2name = 'deleted_at';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Trust checks: email, phone, government ID. Hosts can require a verified ID before accepting a booking.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'identity_verifications';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Verification vendor reference; document images stay with the vendor',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'identity_verifications',
@level2type = N'Column', @level2name = 'provider_reference';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Wishlists.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'saved_listings';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Summer 2027, Ski trip',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'saved_listings',
@level2type = N'Column', @level2name = 'list_name';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A place to stay, owned by one host. Base price, cleaning fee and guest limits live here; adjustments live in pricing_rules and calendar_nights.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Apartment, Cabin, Villa, Houseboat',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings',
@level2type = N'Column', @level2name = 'property_type';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Exact point is shown only to confirmed guests; search shows a blurred area',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings',
@level2type = N'Column', @level2name = 'latitude';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'IANA zone. Check-in times and calendar nights are local to the property.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings',
@level2type = N'Column', @level2name = 'timezone';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '1.5 means one full and one half bath',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings',
@level2type = N'Column', @level2name = 'bathrooms';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Default price for a night with no rule or override',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings',
@level2type = N'Column', @level2name = 'base_nightly_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Charged once per stay',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings',
@level2type = N'Column', @level2name = 'cleaning_fee_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Per night, for each guest above guests_included',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listings',
@level2type = N'Column', @level2name = 'extra_guest_fee_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Listing photos. Dimensions are stored so the page can reserve space before images load.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listing_photos';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Object storage key. Image bytes never live in the database.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listing_photos',
@level2type = N'Column', @level2name = 'storage_key';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Display order; the first is the cover photo',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listing_photos',
@level2type = N'Column', @level2name = 'position';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The fixed amenity catalog. Search filters by code, so a typo by one host never splits a filter.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'amenities';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. wifi, kitchen, hot_tub, ev_charger, step_free_entrance',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'amenities',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Which amenities a listing offers.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listing_amenities';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. 500 Mbps, shared with one other unit',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'listing_amenities',
@level2type = N'Column', @level2name = 'details';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Rules the guest must accept before booking. The booking records when they were accepted.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'house_rules';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. pets: true, smoking: false',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'house_rules',
@level2type = N'Column', @level2name = 'is_allowed';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Quiet hours 22:00 to 08:00, one dog under 20 kg',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'house_rules',
@level2type = N'Column', @level2name = 'details';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Versioned rows, never edited. Changing a policy inserts a new version; a booking points at the exact version it was made under, so the refund a guest was promised can always be recomputed.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellation_policies';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. flexible, moderate, strict, non_refundable',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellation_policies',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Full refund when cancelled at least this many days before check-in',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellation_policies',
@level2type = N'Column', @level2name = 'full_refund_days_before';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Partial refund window, if any',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellation_policies',
@level2type = N'Column', @level2name = 'partial_refund_days_before';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Share of the nightly subtotal refunded in the partial window',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellation_policies',
@level2type = N'Column', @level2name = 'partial_refund_pct';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Refunded whenever the stay has not started',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellation_policies',
@level2type = N'Column', @level2name = 'cleaning_fee_refundable';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One row per listing per night, generated for the bookable window (rolled forward daily). A booking claims its nights with one conditional UPDATE: SET status = booked, booking_id = $1 WHERE listing_id = $2 AND night >= check_in AND night < check_out AND status = available. It must touch exactly as many rows as nights booked, or the transaction rolls back. A night can carry only one booking_id, so two overlapping bookings can never both commit.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'calendar_nights';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The date the guest sleeps there. Check-out day is not a night, so back-to-back stays fit.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'calendar_nights',
@level2type = N'Column', @level2name = 'night';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set while the night is held for a request or booked',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'calendar_nights',
@level2type = N'Column', @level2name = 'booking_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Host-set price for this date; beats every rule',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'calendar_nights',
@level2type = N'Column', @level2name = 'price_override_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. 3-night minimum over a holiday',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'calendar_nights',
@level2type = N'Column', @level2name = 'min_nights_override';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'How a night is priced. Resolution order: calendar override, then the highest-priority seasonal or weekend rule, then base_nightly_cents. Length-of-stay, early bird and last minute rules then discount the nightly subtotal. The cleaning fee is not a rule; it is a fixed per-stay amount on the listing.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Negative for a discount, positive for a markup',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules',
@level2type = N'Column', @level2name = 'adjustment_pct';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Alternative to a percentage: a fixed nightly price, e.g. for a season',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules',
@level2type = N'Column', @level2name = 'nightly_price_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Seasonal rules',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules',
@level2type = N'Column', @level2name = 'start_date';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Weekend rules: bitmask of nights, Friday and Saturday by default',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules',
@level2type = N'Column', @level2name = 'weekend_nights';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Length-of-stay rules: 7 for weekly, 28 for monthly',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules',
@level2type = N'Column', @level2name = 'min_nights';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Early bird (at least) and last minute (at most) rules',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules',
@level2type = N'Column', @level2name = 'days_before_arrival';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Highest wins when two nightly rules overlap',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pricing_rules',
@level2type = N'Column', @level2name = 'priority';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'iCal feeds to and from other sites. Imported busy dates become blocked nights, which is how hosts list on several platforms without double bookings.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'calendar_syncs';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'iCal URL. Treated as a secret: anyone with it can read the calendar.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'calendar_syncs',
@level2type = N'Column', @level2name = 'feed_url';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A stay. Totals here are the sum of booking_nights and booking_charges, frozen when the guest pays.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Copied from the listing, so a listing changing hands never rewrites past stays',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings',
@level2type = N'Column', @level2name = 'host_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Instant books confirm at once; requests wait for the host',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings',
@level2type = N'Column', @level2name = 'mode';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CHECK (check_out > check_in)',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings',
@level2type = N'Column', @level2name = 'check_out';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'What the guest pays: nights plus guest-side charges',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings',
@level2type = N'Column', @level2name = 'guest_total_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'What the host earns: nights plus host-side charges, net of the host fee',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings',
@level2type = N'Column', @level2name = 'host_payout_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The exact policy version in force at booking',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings',
@level2type = N'Column', @level2name = 'cancellation_policy_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Requests auto-decline after 24 hours and release their nights',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bookings',
@level2type = N'Column', @level2name = 'request_expires_at';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The price of every night, as quoted. Editing rules later never changes a confirmed stay, and "why was Saturday more?" has an answer.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'booking_nights';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Snapshot of the resolved nightly price',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'booking_nights',
@level2type = N'Column', @level2name = 'price_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The rule that set it; null for base price or a calendar override',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'booking_nights',
@level2type = N'Column', @level2name = 'pricing_rule_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Every line on both receipts: cleaning fee, extra guests, pets, discounts, service fees and taxes.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'booking_charges';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Guest-side lines add to the guest total; host-side lines reduce the payout',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'booking_charges',
@level2type = N'Column', @level2name = 'payer';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Weekly discount 10%, Cleaning fee, Occupancy tax 6%',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'booking_charges',
@level2type = N'Column', @level2name = 'description';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Negative for discounts',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'booking_charges',
@level2type = N'Column', @level2name = 'amount_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The outcome of a cancellation. The money math is stored, not recomputed later, so a policy change can never alter it.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellations';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null when support cancels on behalf of either side',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellations',
@level2type = N'Column', @level2name = 'cancelled_by_user_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Computed from the policy version on the booking, then frozen',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellations',
@level2type = N'Column', @level2name = 'guest_refund_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'What the host still earns after the refund',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellations',
@level2type = N'Column', @level2name = 'host_payout_kept_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Charged when the host cancels a confirmed stay',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'cancellations',
@level2type = N'Column', @level2name = 'host_penalty_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Guest cards, as processor tokens.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payment_methods';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Card data stays with the payment processor; only its token is stored',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payment_methods',
@level2type = N'Column', @level2name = 'processor_token';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Money in, from the guest, on the guest schedule.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'guest_payments';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Pay part now, part later: 1 at booking, 2 before arrival',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'guest_payments',
@level2type = N'Column', @level2name = 'installment';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Money back to the guest, against a specific payment, since a refund goes back to the card it came from.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'refunds';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Where a host gets paid.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payout_accounts';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Connected-account id; bank details stay with the processor',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payout_accounts',
@level2type = N'Column', @level2name = 'processor_account_token';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Money out, to the host, on the host schedule. Guest money and host money never share a row: the platform holds funds between the guest charge and the host release, so a cancellation before arrival is a refund to the guest, never a clawback from the host.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'host_payouts';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Negative for an adjustment, e.g. a host cancellation penalty',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'host_payouts',
@level2type = N'Column', @level2name = 'amount_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'About 24 hours after check-in, once the guest has arrived',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'host_payouts',
@level2type = N'Column', @level2name = 'release_after';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Two-sided and blind. When the first review of a stay arrives, its published_at is set to the deadline (check-out plus 14 days). When the second arrives, both are set to now(). Readers filter on published_at <= now(), so neither side can read the other first, and nothing has to wake up at the deadline.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reviews';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '1 to 5',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reviews',
@level2type = N'Column', @level2name = 'overall_rating';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Category ratings apply to guest-to-host reviews only',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reviews',
@level2type = N'Column', @level2name = 'cleanliness_rating';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Seen only by the other party, never published',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reviews',
@level2type = N'Column', @level2name = 'private_feedback';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'When it becomes visible; see the table note',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reviews',
@level2type = N'Column', @level2name = 'published_at';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One conversation between a guest and a host about a listing, attached to the booking once there is one.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'message_threads';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for an inquiry before booking',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'message_threads',
@level2type = N'Column', @level2name = 'booking_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Messages in a thread. Kept as the record if a dispute reaches support.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'messages';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for system messages, e.g. booking confirmed',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'messages',
@level2type = N'Column', @level2name = 'sender_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Phone numbers and emails are masked before a booking is confirmed, to keep payment on the platform',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'messages',
@level2type = N'Column', @level2name = 'was_redacted';
GO

ALTER TABLE [identity_verifications] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [saved_listings] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [saved_listings] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [listings] ADD FOREIGN KEY ([host_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [listings] ADD FOREIGN KEY ([cancellation_policy_id]) REFERENCES [cancellation_policies] ([id])
GO

ALTER TABLE [listing_photos] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [listing_amenities] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [listing_amenities] ADD FOREIGN KEY ([amenity_id]) REFERENCES [amenities] ([id])
GO

ALTER TABLE [house_rules] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [calendar_nights] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [calendar_nights] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [pricing_rules] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [calendar_syncs] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [bookings] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [bookings] ADD FOREIGN KEY ([guest_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [bookings] ADD FOREIGN KEY ([host_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [bookings] ADD FOREIGN KEY ([cancellation_policy_id]) REFERENCES [cancellation_policies] ([id])
GO

ALTER TABLE [booking_nights] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [booking_nights] ADD FOREIGN KEY ([pricing_rule_id]) REFERENCES [pricing_rules] ([id])
GO

ALTER TABLE [booking_charges] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [cancellations] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [cancellations] ADD FOREIGN KEY ([cancelled_by_user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [payment_methods] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [guest_payments] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [guest_payments] ADD FOREIGN KEY ([payment_method_id]) REFERENCES [payment_methods] ([id])
GO

ALTER TABLE [refunds] ADD FOREIGN KEY ([guest_payment_id]) REFERENCES [guest_payments] ([id])
GO

ALTER TABLE [refunds] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [payout_accounts] ADD FOREIGN KEY ([host_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [host_payouts] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [host_payouts] ADD FOREIGN KEY ([host_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [host_payouts] ADD FOREIGN KEY ([payout_account_id]) REFERENCES [payout_accounts] ([id])
GO

ALTER TABLE [reviews] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [reviews] ADD FOREIGN KEY ([author_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [reviews] ADD FOREIGN KEY ([subject_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [message_threads] ADD FOREIGN KEY ([listing_id]) REFERENCES [listings] ([id])
GO

ALTER TABLE [message_threads] ADD FOREIGN KEY ([guest_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [message_threads] ADD FOREIGN KEY ([host_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [message_threads] ADD FOREIGN KEY ([booking_id]) REFERENCES [bookings] ([id])
GO

ALTER TABLE [messages] ADD FOREIGN KEY ([thread_id]) REFERENCES [message_threads] ([id])
GO

ALTER TABLE [messages] ADD FOREIGN KEY ([sender_id]) REFERENCES [users] ([id])
GO

How to use this template

  1. Open the ER diagram to see the tables and how they relate.
  2. Copy it into your workspace. You get your own editable copy, in DBML.
  3. Export it as PostgreSQL, MySQL, or SQL Server DDL, or as Prisma, Drizzle, SQLAlchemy, TypeScript, Zod or C# models.

The copy opens in DB Planner’s ER diagram tool, with the schema as text in the DBML editor beside it. Have a database already? Turn its SQL into an ER diagram instead.

Frequently asked questions

Which table is at the centre of the Vacation Rental Marketplace database schema?
users. It is referenced by 11 other tables: bookings, cancellations, host_payouts, identity_verifications, listings and 6 more.
How is bookings related to users?
bookings.guest_id references users.id, a many to one relationship. All 40 relationships in the schema are listed on this page.
Which table in the Vacation Rental Marketplace database schema has the most columns?
listings, with 29 columns, followed by bookings (21) and reviews (15). The schema has 214 columns across 24 tables.
Can I export it to PostgreSQL or MySQL?
Yes. Every DB Planner template exports as DBML, as PostgreSQL, MySQL, or SQL Server DDL, and as C# models.
Is this template free?
Yes. You can open it in the browser without an account, and copy it into your own workspace with a free DB Planner account.
Can I change it after copying it?
Yes. A copy is yours: you can rename tables, add columns, and export it. Changes the author makes later do not affect your copy.