CREATE TABLE attach (id BIGINT AUTO_INCREMENT, estate_id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, filename VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX estate_id_idx (estate_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE building_condition (id BIGINT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE building_type (id BIGINT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE category (id BIGINT AUTO_INCREMENT, parent_id BIGINT, name VARCHAR(255) NOT NULL, slug VARCHAR(255), UNIQUE INDEX category_sluggable_idx (slug), INDEX parent_id_idx (parent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE demand (id BIGINT AUTO_INCREMENT, estate_id BIGINT, estate_name VARCHAR(255) NOT NULL, is_complet TINYINT(1) DEFAULT '0', customer VARCHAR(255) NOT NULL, tel VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, text LONGTEXT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX estate_id_idx (estate_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE estate (id BIGINT AUTO_INCREMENT, category_id BIGINT NOT NULL, sub_category_id BIGINT, type_id BIGINT NOT NULL, change_user_id BIGINT NOT NULL, broker_user_id BIGINT, building_type_id BIGINT, building_condition_id BIGINT, object_type_id BIGINT, price_unit_id BIGINT NOT NULL, ownership_id BIGINT, name VARCHAR(255) NOT NULL, description LONGTEXT, price DOUBLE(18, 2) NOT NULL, city VARCHAR(255) NOT NULL, usable_area BIGINT, estate_area BIGINT, floor_number BIGINT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, slug VARCHAR(255), UNIQUE INDEX estate_sluggable_idx (slug), INDEX category_id_idx (category_id), INDEX sub_category_id_idx (sub_category_id), INDEX type_id_idx (type_id), INDEX change_user_id_idx (change_user_id), INDEX broker_user_id_idx (broker_user_id), INDEX building_type_id_idx (building_type_id), INDEX building_condition_id_idx (building_condition_id), INDEX object_type_id_idx (object_type_id), INDEX price_unit_id_idx (price_unit_id), INDEX ownership_id_idx (ownership_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE image (id BIGINT AUTO_INCREMENT, estate_id BIGINT NOT NULL, filename VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX estate_id_idx (estate_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE object_type (id BIGINT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE ownership (id BIGINT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE page (id BIGINT AUTO_INCREMENT, title VARCHAR(255) NOT NULL, text LONGTEXT, is_deletable TINYINT(1) DEFAULT '1', created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, slug VARCHAR(255), UNIQUE INDEX page_sluggable_idx (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE price_unit (id BIGINT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE sub_category (id BIGINT AUTO_INCREMENT, category_id BIGINT, name VARCHAR(255) NOT NULL, INDEX category_id_idx (category_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE type (id BIGINT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, slug VARCHAR(255), UNIQUE INDEX type_sluggable_idx (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE sa_setting (id BIGINT AUTO_INCREMENT, name VARCHAR(255) NOT NULL UNIQUE, type VARCHAR(255) DEFAULT 'input' NOT NULL, widget_options LONGTEXT, value LONGTEXT, setting_group VARCHAR(255), slug VARCHAR(255), UNIQUE INDEX sa_setting_sluggable_idx (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE sf_guard_forgot_password (id BIGINT AUTO_INCREMENT, user_id BIGINT NOT NULL, unique_key VARCHAR(255), expires_at DATETIME NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX user_id_idx (user_id), PRIMARY KEY(id)) ENGINE = INNODB;
CREATE TABLE sf_guard_group (id BIGINT AUTO_INCREMENT, name VARCHAR(255) UNIQUE, description TEXT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE sf_guard_group_permission (group_id BIGINT, permission_id BIGINT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY(group_id, permission_id)) ENGINE = INNODB;
CREATE TABLE sf_guard_permission (id BIGINT AUTO_INCREMENT, name VARCHAR(255) UNIQUE, description TEXT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE sf_guard_remember_key (id BIGINT AUTO_INCREMENT, user_id BIGINT, remember_key VARCHAR(32), ip_address VARCHAR(50), created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX user_id_idx (user_id), PRIMARY KEY(id)) ENGINE = INNODB;
CREATE TABLE sf_guard_user (id BIGINT AUTO_INCREMENT, first_name VARCHAR(255), last_name VARCHAR(255), email_address VARCHAR(255) NOT NULL UNIQUE, username VARCHAR(128) NOT NULL UNIQUE, algorithm VARCHAR(128) DEFAULT 'sha1' NOT NULL, salt VARCHAR(128), password VARCHAR(128), is_active TINYINT(1) DEFAULT '1', is_super_admin TINYINT(1) DEFAULT '0', last_login DATETIME, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX is_active_idx_idx (is_active), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_czech_ci ENGINE = INNODB;
CREATE TABLE sf_guard_user_group (user_id BIGINT, group_id BIGINT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY(user_id, group_id)) ENGINE = INNODB;
CREATE TABLE sf_guard_user_permission (user_id BIGINT, permission_id BIGINT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY(user_id, permission_id)) ENGINE = INNODB;
ALTER TABLE attach ADD CONSTRAINT attach_estate_id_estate_id FOREIGN KEY (estate_id) REFERENCES estate(id) ON DELETE CASCADE;
ALTER TABLE category ADD CONSTRAINT category_parent_id_category_id FOREIGN KEY (parent_id) REFERENCES category(id) ON DELETE CASCADE;
ALTER TABLE demand ADD CONSTRAINT demand_estate_id_estate_id FOREIGN KEY (estate_id) REFERENCES estate(id) ON DELETE SET NULL;
ALTER TABLE estate ADD CONSTRAINT estate_type_id_type_id FOREIGN KEY (type_id) REFERENCES type(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_sub_category_id_sub_category_id FOREIGN KEY (sub_category_id) REFERENCES sub_category(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_price_unit_id_price_unit_id FOREIGN KEY (price_unit_id) REFERENCES price_unit(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_ownership_id_ownership_id FOREIGN KEY (ownership_id) REFERENCES ownership(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_object_type_id_object_type_id FOREIGN KEY (object_type_id) REFERENCES object_type(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_change_user_id_sf_guard_user_id FOREIGN KEY (change_user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_category_id_category_id FOREIGN KEY (category_id) REFERENCES category(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_building_type_id_building_type_id FOREIGN KEY (building_type_id) REFERENCES building_type(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_building_condition_id_building_condition_id FOREIGN KEY (building_condition_id) REFERENCES building_condition(id) ON DELETE CASCADE;
ALTER TABLE estate ADD CONSTRAINT estate_broker_user_id_sf_guard_user_id FOREIGN KEY (broker_user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;
ALTER TABLE image ADD CONSTRAINT image_estate_id_estate_id FOREIGN KEY (estate_id) REFERENCES estate(id) ON DELETE CASCADE;
ALTER TABLE sub_category ADD CONSTRAINT sub_category_category_id_category_id FOREIGN KEY (category_id) REFERENCES category(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_forgot_password ADD CONSTRAINT sf_guard_forgot_password_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_group_permission ADD CONSTRAINT sf_guard_group_permission_permission_id_sf_guard_permission_id FOREIGN KEY (permission_id) REFERENCES sf_guard_permission(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_group_permission ADD CONSTRAINT sf_guard_group_permission_group_id_sf_guard_group_id FOREIGN KEY (group_id) REFERENCES sf_guard_group(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_remember_key ADD CONSTRAINT sf_guard_remember_key_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_user_group ADD CONSTRAINT sf_guard_user_group_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_user_group ADD CONSTRAINT sf_guard_user_group_group_id_sf_guard_group_id FOREIGN KEY (group_id) REFERENCES sf_guard_group(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_user_permission ADD CONSTRAINT sf_guard_user_permission_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;
ALTER TABLE sf_guard_user_permission ADD CONSTRAINT sf_guard_user_permission_permission_id_sf_guard_permission_id FOREIGN KEY (permission_id) REFERENCES sf_guard_permission(id) ON DELETE CASCADE;
