-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema rta
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema rta
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `rta` DEFAULT CHARACTER SET utf8 ;
USE `rta` ;

-- -----------------------------------------------------
-- Table `rta`.`address`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`address` ;

CREATE TABLE IF NOT EXISTS `rta`.`address` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `address_line_1` VARCHAR(256) NULL,
  `address_line_2` VARCHAR(256) NULL,
  `city` VARCHAR(45) NOT NULL,
  `state` VARCHAR(45) NOT NULL,
  `district_id` INT NULL,
  `country` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`party`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`party` ;

CREATE TABLE IF NOT EXISTS `rta`.`party` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `party_type` VARCHAR(45) NOT NULL,
  `email` VARCHAR(45) NULL,
  `phone_number_1` VARCHAR(45) NOT NULL,
  `phone_number_2` VARCHAR(45) NULL,
  `address_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_party_address1_idx` (`address_id` ASC),
  CONSTRAINT `fk_party_address1`
    FOREIGN KEY (`address_id`)
    REFERENCES `rta`.`address` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`service`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`service` ;

CREATE TABLE IF NOT EXISTS `rta`.`service` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `description` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`service_purchase`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`service_purchase` ;

CREATE TABLE IF NOT EXISTS `rta`.`service_purchase` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `party_id` INT NOT NULL,
  `service_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_service_purchase_party1_idx` (`party_id` ASC),
  INDEX `fk_service_purchase_service1_idx` (`service_id` ASC),
  CONSTRAINT `fk_service_purchase_party1`
    FOREIGN KEY (`party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_servicet_purchase_service1`
    FOREIGN KEY (`service_id`)
    REFERENCES `rta`.`service` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`workflow`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`workflow` ;

CREATE TABLE IF NOT EXISTS `rta`.`workflow` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `container_id` VARCHAR(45) NULL,
  `definition_id` VARCHAR(45) NULL,
  `version` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`service_workflow`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`service_workflow` ;

CREATE TABLE IF NOT EXISTS `rta`.`service_workflow` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `workflow_id` INT NOT NULL,
  `service_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_service_workflow_workflow1_idx` (`workflow_id` ASC),
  INDEX `fk_servicet_workflow_service1_idx` (`service_id` ASC),
  CONSTRAINT `fk_service_workflow_workflow1`
    FOREIGN KEY (`workflow_id`)
    REFERENCES `rta`.`workflow` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_service_workflow_service1`
    FOREIGN KEY (`service_id`)
    REFERENCES `rta`.`service` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`transaction_workflow`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`transaction_workflow` ;

CREATE TABLE IF NOT EXISTS `rta`.`transaction_workflow` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `workflow_status` VARCHAR(45) NOT NULL,
  `workflow_task_id` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`transaction`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`transaction` ;

CREATE TABLE IF NOT EXISTS `rta`.`transaction` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `service_purchase_id` INT NOT NULL,
  `service_workflow_id` INT NOT NULL,
  `assigned_to` INT NOT NULL,
  `transaction_workflow_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_transaction_service_purchase1_idx` (`service_purchase_id` ASC),
  INDEX `fk_transaction_service_workflow1_idx` (`service_workflow_id` ASC),
  INDEX `fk_transaction_transaction_workflow1_idx` (`transaction_workflow_id` ASC),
  CONSTRAINT `fk_transaction_service_purchase1`
    FOREIGN KEY (`service_purchase_id`)
    REFERENCES `rta`.`service_purchase` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_transaction_service_workflow1`
    FOREIGN KEY (`service_workflow_id`)
    REFERENCES `rta`.`service_workflow` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_transaction_transaction_workflow1`
    FOREIGN KEY (`transaction_workflow_id`)
    REFERENCES `rta`.`transaction_workflow` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`invoice`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`invoice` ;

CREATE TABLE IF NOT EXISTS `rta`.`invoice` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `transaction_id` INT NOT NULL,
  `invoice_amount` DECIMAL(10,2) NOT NULL,
  `invoice_date` DATETIME NOT NULL,
  `due_date` DATETIME NOT NULL,
  `outstanding_balance` DECIMAL(10,2) NOT NULL DEFAULT 0,
  `tax_amount` DECIMAL(10,2) NULL,
  `total_invoice_amount` DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_invoice_transaction1_idx` (`transaction_id` ASC),
  CONSTRAINT `fk_invoice_transaction1`
    FOREIGN KEY (`transaction_id`)
    REFERENCES `rta`.`transaction` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`payment_method`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`payment_method` ;

CREATE TABLE IF NOT EXISTS `rta`.`payment_method` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `payment_method` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`payment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`payment` ;

CREATE TABLE IF NOT EXISTS `rta`.`payment` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `invoice_id` INT NOT NULL,
  `payment_amount` DECIMAL(10,2) NOT NULL,
  `payment_date` DATETIME NULL,
  `external_payment_reference_no` VARCHAR(45) NULL,
  `payment_method_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_payment_invoice1_idx` (`invoice_id` ASC),
  INDEX `fk_payment_payment_method1_idx` (`payment_method_id` ASC),
  CONSTRAINT `fk_payment_invoice1`
    FOREIGN KEY (`invoice_id`)
    REFERENCES `rta`.`invoice` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_payment_payment_method1`
    FOREIGN KEY (`payment_method_id`)
    REFERENCES `rta`.`payment_method` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`person`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`person` ;

CREATE TABLE IF NOT EXISTS `rta`.`person` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(20) NOT NULL,
  `second_name` VARCHAR(20) NOT NULL,
  `third_name` VARCHAR(20) NULL,
  `fourth_name` VARCHAR(20) NULL,
  `date_of_birth` DATETIME NOT NULL,
  `gender` VARCHAR(10) NOT NULL,
  `occupation` VARCHAR(45) NULL,
  `place_of_birth` VARCHAR(45) NOT NULL,
  `height` VARCHAR(45) NOT NULL,
  `weight` VARCHAR(45) NOT NULL,
  `eye_color` VARCHAR(45) NOT NULL,
  `hair_color` VARCHAR(45) NOT NULL,
  `mother_first_name` VARCHAR(20) NULL,
  `mother_second_name` VARCHAR(20) NULL,
  `mother_third_name` VARCHAR(20) NULL,
  `mother_fourth_name` VARCHAR(20) NULL,
  `father_first_name` VARCHAR(20) NULL,
  `father_second_name` VARCHAR(20) NULL,
  `father_third_name` VARCHAR(20) NULL,
  `father_fourth_name` VARCHAR(20) NULL,
  `party_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_person_party1_idx` (`party_id` ASC),
  CONSTRAINT `fk_person_party1`
    FOREIGN KEY (`party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`company_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`company_type` ;

CREATE TABLE IF NOT EXISTS `rta`.`company_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`company`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`company` ;

CREATE TABLE IF NOT EXISTS `rta`.`company` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `company_name` VARCHAR(45) NOT NULL,
  `contact_party_id` INT NOT NULL,
  `company_type_id` INT NOT NULL,
  `registration_date` DATETIME NOT NULL,
  `party_id` INT NOT NULL,
  INDEX `fk_company_party1_idx` (`party_id` ASC),
  PRIMARY KEY (`id`),
  INDEX `fk_company_person1_idx` (`contact_party_id` ASC),
  CONSTRAINT `fk_company_party1`
    FOREIGN KEY (`party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_company_person1`
    FOREIGN KEY (`contact_party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_company_company_type2`
    FOREIGN KEY (`company_type_id`)
    REFERENCES `rta`.`company_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`document_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`document_type` ;

CREATE TABLE IF NOT EXISTS `rta`.`document_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`document`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`document` ;

CREATE TABLE IF NOT EXISTS `rta`.`document` (
  `id` INT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `uri` VARCHAR(45) NOT NULL,
  `status` VARCHAR(45) NULL,
  `document_type_id` INT NOT NULL,
  `documentable_id` INT NOT NULL,
  `documentable_type` VARCHAR(45) NOT NULL,
  INDEX `fk_document_document_type1_idx` (`document_type_id` ASC),
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_document_document_type1`
    FOREIGN KEY (`document_type_id`)
    REFERENCES `rta`.`document_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`district`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`district` ;

CREATE TABLE IF NOT EXISTS `rta`.`district` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `district` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`region`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`region` ;

CREATE TABLE IF NOT EXISTS `rta`.`region` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `district_id` INT NOT NULL,
  `region` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_region_district1_idx` (`district_id` ASC),
  CONSTRAINT `fk_region_district1`
    FOREIGN KEY (`district_id`)
    REFERENCES `rta`.`district` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`frequency_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`frequency_type` ;

CREATE TABLE IF NOT EXISTS `rta`.`frequency_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `frequency` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`notification_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`notification_type` ;

CREATE TABLE IF NOT EXISTS `rta`.`notification_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `notification_type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`comment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`comment` ;

CREATE TABLE IF NOT EXISTS `rta`.`comment` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `comment` LONGBLOB NOT NULL,
  `commentable_id` INT NOT NULL,
  `commentable_type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`sub_category`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`sub_category` ;

CREATE TABLE IF NOT EXISTS `rta`.`sub_category` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `sub_category` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`purpose_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`purpose_type` ;

CREATE TABLE IF NOT EXISTS `rta`.`purpose_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `purpose` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`service_fee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`service_fee` ;

CREATE TABLE IF NOT EXISTS `rta`.`service_fee` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `sub_category_id` INT NULL,
  `district_id` INT NULL,
  `region_id` INT NULL,
  `frequency_type_id` INT NOT NULL,
  `fee` DECIMAL(10,2) NOT NULL,
  `purpose_type_id` INT NULL,
  `recurring` VARCHAR(45) NULL DEFAULT 'N',
  `service_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_service_fee_sub_category1_idx` (`sub_category_id` ASC),
  INDEX `fk_service_fee_district1_idx` (`district_id` ASC),
  INDEX `fk_service_fee_region1_idx` (`region_id` ASC),
  INDEX `fk_service_fee_frequency_type1_idx` (`frequency_type_id` ASC),
  INDEX `fk_service_fee_purpose_type1_idx` (`purpose_type_id` ASC),
  INDEX `fk_service_fee_service1_idx` (`service_id` ASC),
  CONSTRAINT `fk_service_fee_sub_category1`
    FOREIGN KEY (`sub_category_id`)
    REFERENCES `rta`.`sub_category` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_service_fee_district1`
    FOREIGN KEY (`district_id`)
    REFERENCES `rta`.`district` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_service_fee_region1`
    FOREIGN KEY (`region_id`)
    REFERENCES `rta`.`region` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_service_fee_frequency_type1`
    FOREIGN KEY (`frequency_type_id`)
    REFERENCES `rta`.`frequency_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_service_fee_purpose_type1`
    FOREIGN KEY (`purpose_type_id`)
    REFERENCES `rta`.`purpose_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_service_fee_service1`
    FOREIGN KEY (`service_id`)
    REFERENCES `rta`.`service` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`make`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`make` ;

CREATE TABLE IF NOT EXISTS `rta`.`make` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`model`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`model` ;

CREATE TABLE IF NOT EXISTS `rta`.`model` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `make_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_model_make1_idx` (`make_id` ASC),
  CONSTRAINT `fk_model_make1`
    FOREIGN KEY (`make_id`)
    REFERENCES `rta`.`make` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`vehicle_color`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`vehicle_color` ;

CREATE TABLE IF NOT EXISTS `rta`.`vehicle_color` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `color` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`fuel_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`fuel_type` ;

CREATE TABLE IF NOT EXISTS `rta`.`fuel_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `fuel_type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`vehicle_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`vehicle_type` ;

CREATE TABLE IF NOT EXISTS `rta`.`vehicle_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `vehicle_type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`vehicle`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`vehicle` ;

CREATE TABLE IF NOT EXISTS `rta`.`vehicle` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `odometer` BIGINT NOT NULL,
  `vin` VARCHAR(45) NOT NULL,
  `year` INT NOT NULL,
  `sub_category_id` INT NOT NULL,
  `smart_tag` VARCHAR(45) NULL,
  `model_id` INT NOT NULL,
  `vehicle_color_id` INT NOT NULL,
  `fuel_type_id` INT NOT NULL,
  `vehicle_type_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_vehicle_sub_category1_idx` (`sub_category_id` ASC),
  INDEX `fk_vehicle_model1_idx` (`model_id` ASC),
  INDEX `fk_vehicle_vehicle_color1_idx` (`vehicle_color_id` ASC),
  INDEX `fk_vehicle_fuel_type1_idx` (`fuel_type_id` ASC),
  INDEX `fk_vehicle_vehicle_type1_idx` (`vehicle_type_id` ASC),
  CONSTRAINT `fk_vehicle_sub_category1`
    FOREIGN KEY (`sub_category_id`)
    REFERENCES `rta`.`sub_category` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_vehicle_model1`
    FOREIGN KEY (`model_id`)
    REFERENCES `rta`.`model` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_vehicle_vehicle_color1`
    FOREIGN KEY (`vehicle_color_id`)
    REFERENCES `rta`.`vehicle_color` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_vehicle_fuel_type1`
    FOREIGN KEY (`fuel_type_id`)
    REFERENCES `rta`.`fuel_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_vehicle_vehicle_type1`
    FOREIGN KEY (`vehicle_type_id`)
    REFERENCES `rta`.`vehicle_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`party_vehicle`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`party_vehicle` ;

CREATE TABLE IF NOT EXISTS `rta`.`party_vehicle` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `party_id` INT NOT NULL,
  `vehicle_id` INT NOT NULL,
  `purpose_type_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_party_vehicle_party1_idx` (`party_id` ASC),
  INDEX `fk_party_vehicle_vehicle1_idx` (`vehicle_id` ASC),
  INDEX `fk_party_vehicle_purpose_type1_idx` (`purpose_type_id` ASC),
  CONSTRAINT `fk_party_vehicle_party1`
    FOREIGN KEY (`party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_party_vehicle_vehicle1`
    FOREIGN KEY (`vehicle_id`)
    REFERENCES `rta`.`vehicle` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_party_vehicle_purpose_type1`
    FOREIGN KEY (`purpose_type_id`)
    REFERENCES `rta`.`purpose_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`party_vehicle_purchase`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`party_vehicle_purchase` ;

CREATE TABLE IF NOT EXISTS `rta`.`party_vehicle_purchase` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `purchase_date` DATETIME NULL,
  `sale_date` DATETIME NULL,
  `purchase_price` DECIMAL(10,2) NULL,
  `sale_price` DECIMAL(10,2) NULL,
  `buyer_party_id` INT NOT NULL,
  `vehicle_id` INT NOT NULL,
  `seller_party_id` INT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_party_vehicle_purchase_party1_idx` (`buyer_party_id` ASC),
  INDEX `fk_party_vehicle_purchase_vehicle1_idx` (`vehicle_id` ASC),
  INDEX `fk_party_vehicle_purchase_party2_idx` (`seller_party_id` ASC),
  CONSTRAINT `fk_party_vehicle_purchase_party1`
    FOREIGN KEY (`buyer_party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_party_vehicle_purchase_vehicle1`
    FOREIGN KEY (`vehicle_id`)
    REFERENCES `rta`.`vehicle` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_party_vehicle_purchase_party2`
    FOREIGN KEY (`seller_party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`notification`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`notification` ;

CREATE TABLE IF NOT EXISTS `rta`.`notification` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `notification_type_id` INT NOT NULL,
  `notifiable_id` INT NOT NULL,
  `notifiable_type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_notification_notification_type1_idx` (`notification_type_id` ASC),
  CONSTRAINT `fk_notification_notification_type1`
    FOREIGN KEY (`notification_type_id`)
    REFERENCES `rta`.`notification_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`vehicle_title`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`vehicle_title` ;

CREATE TABLE IF NOT EXISTS `rta`.`vehicle_title` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `title_number` VARCHAR(45) NOT NULL,
  `issued_date` DATETIME NULL,
  `vehicle_id` INT NOT NULL,
  `party_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_vehicle_title_vehicle1_idx` (`vehicle_id` ASC),
  INDEX `fk_vehicle_title_party1_idx` (`party_id` ASC),
  CONSTRAINT `fk_vehicle_title_vehicle1`
    FOREIGN KEY (`vehicle_id`)
    REFERENCES `rta`.`vehicle` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_vehicle_title_party1`
    FOREIGN KEY (`party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`term`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`term` ;

CREATE TABLE IF NOT EXISTS `rta`.`term` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `year` INT NOT NULL,
  `quarter` INT NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rta`.`vehicle_tax`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `rta`.`vehicle_tax` ;

CREATE TABLE IF NOT EXISTS `rta`.`vehicle_tax` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `tax_number` VARCHAR(45) NOT NULL,
  `status` VARCHAR(45) NULL,
  `term_id` INT NOT NULL,
  `vehicle_id` INT NOT NULL,
  `party_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_vehicle_tax_term1_idx` (`term_id` ASC),
  INDEX `fk_vehicle_tax_vehicle1_idx` (`vehicle_id` ASC),
  INDEX `fk_vehicle_tax_party1_idx` (`party_id` ASC),
  CONSTRAINT `fk_vehicle_tax_term1`
    FOREIGN KEY (`term_id`)
    REFERENCES `rta`.`term` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_vehicle_tax_vehicle1`
    FOREIGN KEY (`vehicle_id`)
    REFERENCES `rta`.`vehicle` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_vehicle_tax_party1`
    FOREIGN KEY (`party_id`)
    REFERENCES `rta`.`party` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
