-- Deploy gaia:api_2016_12_1_create_table_for_fenxiao to mysql

BEGIN;

CREATE TABLE `api_fenxiaocompany` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `created_at` datetime(6),
    `is_online` bool NOT NULL,
    `name` varchar(200) NOT NULL UNIQUE,
    `back_rate` integer NOT NULL,
    `pay_type` varchar(4) NOT NULL,
    `back_month_limit` integer DEFAULT NULL,
    `back_order_limit` integer DEFAULT NULL
)
;
CREATE TABLE `api_fenxiaowanghong` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `code` varchar(8) NOT NULL UNIQUE,
    `created_at` datetime(6) NOT NULL,
    `is_online` bool NOT NULL,
    `company_id` integer NOT NULL
)
;
ALTER TABLE `api_fenxiaowanghong` ADD CONSTRAINT `company_id_refs_id_c1390bd9` FOREIGN KEY (`company_id`) REFERENCES `api_fenxiaocompany` (`id`);
CREATE TABLE `api_fenxiaopersonwanghong` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `created_at` datetime(6) NOT NULL,
    `person_id` char(32) NOT NULL UNIQUE,
    `wanghong_id` integer NOT NULL
)
;
ALTER TABLE `api_fenxiaopersonwanghong` ADD CONSTRAINT `wanghong_id_refs_id_c58ecac2` FOREIGN KEY (`wanghong_id`) REFERENCES `api_fenxiaowanghong` (`id`);
ALTER TABLE `api_fenxiaopersonwanghong` ADD CONSTRAINT `person_id_refs_id_a206efd0` FOREIGN KEY (`person_id`) REFERENCES `api_person` (`id`);
CREATE TABLE `api_fenxiaoorderwanghong` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `order_id` varchar(12) NOT NULL UNIQUE,
    `person_id` char(32) NOT NULL,
    `wanghong_id` integer NOT NULL,
    `back_rate` integer NOT NULL,
    `money_received_cent` integer NOT NULL,
    `pay_type` varchar(4) NOT NULL
)
;
ALTER TABLE `api_fenxiaoorderwanghong` ADD CONSTRAINT `wanghong_id_refs_id_423c46f7` FOREIGN KEY (`wanghong_id`) REFERENCES `api_fenxiaowanghong` (`id`);
ALTER TABLE `api_fenxiaoorderwanghong` ADD CONSTRAINT `person_id_refs_id_acfa652b` FOREIGN KEY (`person_id`) REFERENCES `api_person` (`id`);
ALTER TABLE `api_fenxiaoorderwanghong` ADD CONSTRAINT `order_id_refs_id_6af04914` FOREIGN KEY (`order_id`) REFERENCES `api_order` (`id`);

COMMIT;