having trouble with my database using workbench - mysql

I am creating a test database to train with and am running into this problem -
"Schema Creation Failed: Can't create table 'db_2_5b129.tbluserassignment' (errno: 150): "
The code for the specific table is -
-- Table tblUserAssignment
CREATE TABLE IF NOT EXISTS `tblUserAssignment` (
`assignment_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`supervisor_id` INT NOT NULL,
`position_id` INT NOT NULL,
`department_id` INT NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
`date_added` DATE NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`assignment_id`),
INDEX `fk_tblUserAssignment_tblUserPhone1_idx` (`user_id` ASC),
INDEX `fk_tblUserAssignment_tblUserPositions1_idx` (`position_id` ASC),
CONSTRAINT `fk_tblUserAssignment_tblUserPhone1`
FOREIGN KEY (`user_id`)
REFERENCES `tblUserPhone` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tblUserAssignment_tblUserPositions1`
FOREIGN KEY (`position_id`)
REFERENCES `tblUserPositions` (`position_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Can someone look it over and point me in the right direction?

problem is in differences between forign fields type
tblUserPositions.position_id INT UNSIGNED NOT NULL AUTO_INCREMEN
and
tblUserAssignment.position_id INT NOT NULL,`
EDIT 1
see this Use Composite Primary Key as Foreign Key
EDIT 2
this one tested & works fine
CREATE TABLE `tbluserphone` (
`contact_information_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(11) NOT NULL,
`phone_number` VARCHAR(20) NOT NULL,
`phone_type_id` INT(11) NOT NULL,
`date_added` DATETIME NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`contact_information_id`, `user_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
CREATE TABLE `tbluserassignment` (
`assignment_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`contact_information_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(11) NOT NULL,
`supervisor_id` INT(11) NOT NULL,
`position_id` INT(10) UNSIGNED NOT NULL,
`department_id` INT(11) NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
`date_added` DATE NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`assignment_id`),
INDEX `fk_tblUserAssignment_tblUserPhone1_idx` (`user_id`),
INDEX `fk_tblUserAssignment_tblUserPositions1_idx` (`position_id`),
INDEX `contact` (`contact_information_id`),
CONSTRAINT `FK_tbluserassignment_tbluserpositions`
FOREIGN KEY (`position_id`)
REFERENCES `tbluserpositions` (`position_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT tbluserassignment_fkz1
FOREIGN KEY (contact_information_id, user_id)
REFERENCES tbluserphone (contact_information_id, user_id)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
(contact_information_id, user_id) - fields should goes same way, like in primary key difinition

Related

Foreign key referring to primary key in the same table Mysql

# Sql to create userdetails table:
CREATE TABLE `userdetails` (
`user_details_id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`user_group_id` int(1) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`email_id` varchar(50),
`password` varchar(50) NOT NULL,
`mobile_no` varchar(10) NOT NULL,
`company_id` int(3) unsigned,
`vehicle_id` varchar(10),
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_updated` datetime NOT NULL,
`created_by` int(3) unsigned NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`user_details_id`),
UNIQUE KEY `email_id` (`email_id`),
FOREIGN KEY (`user_group_id`) REFERENCES `usergroups` (`user_group_id`),
FOREIGN KEY (`company_id`) REFERENCES `companies` (`company_id`),
FOREIGN KEY (`created_by`) REFERENCES `userdetails` (`user_details_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Focusing on the 'user_details_id' and 'created_by' columns, the conflict arises when created_by is not referring to an existing user_details_id.
Meaning, if I am creating my own profile with the required details, my user_details_id has not been generated yet and hence I do not know what value to input into the created_by field (which is ideally supposed to contain my user_details_id).
I would appreciate if anyone can guide me in the right direction on how to approach such a conflict, and if there is a way to determine what the value of the user_details_id field could be before it's even generated.
Thanks.
You can't handle it by temporarily disabling foreign key checks by setting server variable foreign_key_checks
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO cities userdetails(....);
SET FOREIGN_KEY_CHECKS = 1;
# Sql to create userdetails table:
CREATE TABLE `userdetails` (
`user_details_id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`user_group_id` int(1) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`email_id` varchar(50),
`password` varchar(50) NOT NULL,
`mobile_no` varchar(10) NOT NULL,
`company_id` int(3) unsigned,
`vehicle_id` varchar(10),
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_updated` datetime NOT NULL,
`created_by` int(3) unsigned,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`user_details_id`),
UNIQUE KEY `email_id` (`email_id`),
FOREIGN KEY (`user_group_id`) REFERENCES `usergroups` (`user_group_id`),
FOREIGN KEY (`company_id`) REFERENCES `companies` (`company_id`),
FOREIGN KEY (`created_by`) REFERENCES `userdetails` (`user_details_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Allowing created_by to accept NULL values seems to be a quick fix to this problem as that can imply user is self-created.

MySQL "Foreign key constraint is incorrectly formed"

I have the follwing SQL commands:
CREATE TABLE IF NOT EXISTS `users`
(
`id` INTEGER NOT NULL auto_increment,
`username` VARCHAR(255) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`root` TINYINT(1) DEFAULT 0,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
UNIQUE `username_unique` (`username`),
PRIMARY KEY (`id`)
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `domains`
(
`id` INTEGER NOT NULL auto_increment,
`domain` VARCHAR(255) NOT NULL UNIQUE,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
`userid` INTEGER,
UNIQUE `domain_unique` (`domain`),
PRIMARY KEY (`id`),
FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE SET NULL ON
UPDATE CASCADE
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `aliases`
(
`id` INTEGER NOT NULL auto_increment,
`source_username` VARCHAR(255) NOT NULL,
`source_domain` VARCHAR(255) NOT NULL,
`destination_username` VARCHAR(255) NOT NULL,
`destination_domain` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`source_domain`) REFERENCES `domains` (`domain`)
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `accounts`
(
`id` INTEGER NOT NULL auto_increment,
`username` VARCHAR(255) NOT NULL,
`domain` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`quota` INTEGER NOT NULL DEFAULT 500,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`sendonly` TINYINT(1) NOT NULL DEFAULT 0,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`domain`) REFERENCES `domains` (`domain`)
)
engine=innodb;
if i try to run these i get the message:
"Foreign key constraint is incorrectly formed"
for the tables Alias and Accounts.
Its not possible to use primary keys in alias and accounts
Im using MariaDB 10.2
I hope somebody can tell me what is wrong with these statements.
You need to add a key/index to the domain field of the domains table, in order for it to be used as Foreign Key by another table.
If the table is already created and you want to add an index, use this.
CREATE INDEX domain ON domains(domain);
OR add the index while creating the table -
CREATE TABLE IF NOT EXISTS `domains`
(
`id` INTEGER NOT NULL auto_increment,
`domain` VARCHAR(255) NOT NULL UNIQUE,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
`userid` INTEGER,
UNIQUE `domain_unique` (`domain`),
PRIMARY KEY (`id`),
INDEX domain (domain),
FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE SET NULL ON
UPDATE CASCADE
)
engine=innodb;
For further reading (official documentation) regarding Foreign Key Contraint Errors on MariaDB this URL
You define an integer primary key. Use it:
CREATE TABLE IF NOT EXISTS `aliases`
(
`id` INTEGER NOT NULL auto_increment,
`source_username` VARCHAR(255) NOT NULL,
`source_domain_id` INTEGER NOT NULL,
`destination_username` VARCHAR(255) NOT NULL,
`destination_domain` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`source_domain_id`) REFERENCES `domains` (`id`)
)
engine=innodb;

SQL Foreign Key

I'm reading a book about e-commerce. In this book I found the folowing SQL code:
CREATE TABLE `orders` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`users_id` INT UNSIGNED NOT NULL,
`transaction_id` VARCHAR(45) NOT NULL,
`payment_status` VARCHAR(45) NOT NULL,
`payment_amount` INT UNSIGNED NOT NULL,
`date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `date_created` (`date_created` ASC),
INDEX `transaction_id` (`transaction_id` ASC),
CONSTRAINT `fk_orders_users1`
FOREIGN KEY (`id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
The book says that the CONSTRAINT would prevent the insertion of a "users_id" that doesn't match a 'id' in the table 'users'. By reading the code, I believe the book is wrong, because I believe this code would acctualy prevent the insertion of a "id" (in the table "orders") that doesn't match the field "id" (in the table users). I am right?
Sorry for bad english. I'm not american and trying my best...
Corrected code:
CREATE TABLE `orders` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`users_id` INT UNSIGNED NOT NULL,
`transaction_id` VARCHAR(45) NOT NULL,
`payment_status` VARCHAR(45) NOT NULL,
`payment_amount` INT UNSIGNED NOT NULL,
`date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `date_created` (`date_created` ASC),
INDEX `transaction_id` (`transaction_id` ASC),
CONSTRAINT `fk_orders_users1`
FOREIGN KEY (`users_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE = InnoDB DEFAULT CHARSET=utf8;

Errors Adding Foreign Keys (MySQL) (Error Code 1215)

I am trying to add a foreign key between two different sets of tables, the first set is customer and shopping_cart. I tried looking at the other posts regarding this error but I couldn't get it to work after looking at them.
CREATE TABLE `usale`.`customer` (
CREATE TABLE `usale`.`customer` (
`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`)),
INDEX `fk_customer_student1_idx` (`student_student_id` ASC,`student_school_id` ASC),
INDEX `fk_customer_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
CONSTRAINT `fk_customer_student1`
FOREIGN KEY (`student_student_id`)
REFERENCES `usale`.`student` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart` (`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE customer ADD CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I'm getting the error: "ERROR: Cannot add foreign key constraint
Error Code: 1215"
For the second set of tables they are named shopping_cart and shopping_cart_item
CREATE TABLE IF NOT EXISTS `usale`.`shopping_cart_item` (
`shopping_cart_item_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
`inventory_item_inventory_item_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_student_id` BIGINT NOT NULL,
`inventory_item_inventory_type` VARCHAR(45) NOT NULL,
`inventory_item_student_student_id` BIGINT NOT NULL,
`inventory_item_student_customer_id` BIGINT NOT NULL,
`inventory_item_student_school_id` BIGINT NOT NULL,
`inventory_item_edition_edition_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_edition_author_id` BIGINT NOT NULL,
PRIMARY KEY (`shopping_cart_item_id`, `shopping_cart_cart_id`, `inventory_item_inventory_item_id`, `inventory_item_student_id`, `inventory_item_inventory_type`, `inventory_item_student_student_id`, `inventory_item_student_customer_id`, `inventory_item_student_school_id`, `inventory_item_edition_edition_id`, `inventory_item_edition_author_id`),
INDEX `fk_shopping_cart_items_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
INDEX `fk_shopping_cart_item_inventory_item1_idx` (`inventory_item_inventory_item_id` ASC, `inventory_item_student_id` ASC, `inventory_item_inventory_type` ASC, `inventory_item_student_student_id` ASC, `inventory_item_student_customer_id` ASC, `inventory_item_student_school_id` ASC, `inventory_item_edition_edition_id` ASC, `inventory_item_edition_author_id` ASC))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE shopping_cart_item ADD CONSTRAINT `fk_shopping_cart_items_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Once again I am getting error 1215: "ERROR: Cannot add foreign key constraint
Error Code: 1215"

Implementing relationships in CakePHP Model

I am new to Cake and am trying to implement the Model layer as best as I can by using the bake script.
The implementation of my DB relationships has me a little worried though.
CREATE TABLE IF NOT EXISTS `model_base_test`.`entrys` (
`id` INT NOT NULL AUTO_INCREMENT,
`entry_year` INT(2) NOT NULL,
`contact_id` INT NOT NULL,
`division_id` INT NOT NULL,
`category_id` INT NOT NULL,
`entry_name` VARCHAR(45) NULL DEFAULT NULL,
`scale` VARCHAR(10) NULL DEFAULT NULL,
`ootb` TINYINT(1) NULL DEFAULT NULL,
`scratch` TINYINT(1) NULL DEFAULT NULL,
`theme` TINYINT(1) NULL DEFAULT NULL,
`score` DECIMAL(4,2) NULL DEFAULT NULL,
`medal_id` INT NULL DEFAULT NULL,
`image_id` INT NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `fk_entries_contacts_idx` (`contact_id` ASC),
INDEX `fk_entries_groups_idx` (`category_id` ASC),
INDEX `fk_entries_medals_idx` (`medal_id` ASC),
UNIQUE INDEX `entry_id_UNIQUE` (`id` ASC),
INDEX `fk_entrys_divisions_idx` (`division_id` ASC),
CONSTRAINT `fk_entries_contacts`
FOREIGN KEY (`contact_id`)
REFERENCES `model_base_test`.`contacts` (`id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_entries_groups`
FOREIGN KEY (`category_id`)
REFERENCES `model_base_test`.`categorys` (`id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_entries_medals`
FOREIGN KEY (`medal_id`)
REFERENCES `model_base_test`.`medals` (`id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_entrys_divisions`
FOREIGN KEY (`division_id`)
REFERENCES `model_base_test`.`divisions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `model_base_test`.`scores` (
`id` INT NOT NULL AUTO_INCREMENT,
`judge_id` INT NOT NULL,
`entry_id` INT NOT NULL,
`criteria` VARCHAR(15) NULL DEFAULT NULL,
`possible_points` INT NULL DEFAULT NULL,
`entry_possible` TINYINT(1) NULL DEFAULT NULL,
`raw_score` INT NULL DEFAULT NULL,
`score_adjustment` INT NULL DEFAULT NULL,
`weighted_score` DECIMAL(5,2) NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `fk_scores_entries_idx` (`entry_id` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC),
CONSTRAINT `fk_scores_entries1`
FOREIGN KEY (`entry_id`)
REFERENCES `model_base_test`.`entrys` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
With the above tables and constraints as example, how best can I implement with Cake? The above is an optional one-to-many relationship, i.e. there is not necessarily a Score row for the mandatory Entry row.