

CREATE TABLE `form`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(512),
	`source_conf` TEXT,
	`description` TEXT,
	`hierarchy_id` INTEGER  NOT NULL,
	`available` INTEGER  DEFAULT 0,
	`show_with_templates` INTEGER  DEFAULT 0,
	`show_with_forms` INTEGER  DEFAULT 0,
    `created_at` datetime NOT NULL,
    `updated_at` datetime NOT NULL,
	PRIMARY KEY (`id`),
	INDEX `form_FI_1` (`hierarchy_id`),
	CONSTRAINT `form_FK_1`
		FOREIGN KEY (`hierarchy_id`)
		REFERENCES `hierarchy` (`id`)
		ON UPDATE RESTRICT
		ON DELETE CASCADE
);



CREATE TABLE `form_tpl_tag`
(
	`form_id` INTEGER  NOT NULL,
	`tpl_tag_id` INTEGER  NOT NULL,
	PRIMARY KEY (`form_id`,`tpl_tag_id`),
	CONSTRAINT `form_tpl_tag_FK_1`
		FOREIGN KEY (`form_id`)
		REFERENCES `form` (`id`)
		ON UPDATE RESTRICT
		ON DELETE CASCADE,
	INDEX `form_tpl_tag_FI_2` (`tpl_tag_id`),
	CONSTRAINT `form_tpl_tag_FK_2`
		FOREIGN KEY (`tpl_tag_id`)
		REFERENCES `tpl_tag` (`id`)
		ON UPDATE RESTRICT
		ON DELETE CASCADE
);



CREATE TABLE `form_workflow`
(
	`form_id` INTEGER  NOT NULL,
	`workflow_id` INTEGER  NOT NULL,
	PRIMARY KEY (`form_id`,`workflow_id`),
	CONSTRAINT `form_workflow_FK_1`
		FOREIGN KEY (`form_id`)
		REFERENCES `template` (`id`)
		ON UPDATE RESTRICT
		ON DELETE CASCADE,
	INDEX `form_workflow_FI_2` (`workflow_id`),
	CONSTRAINT `form_workflow_FK_2`
		FOREIGN KEY (`workflow_id`)
		REFERENCES `workflow` (`id`)
		ON DELETE CASCADE
);