Missing index for constraint - mysql

Getting this error - "Failed to add the foreign key constraint. Missing index for constraint 'FK_transcriptionServiceName' in the referenced table 'transcriptionservices' "
I have searched google to no avail. Any ideas?
Here's the SQL:
CREATE TABLE transcriptionConfig (
id BIGINT,
transcriptionEnabled BOOLEAN,
PRIMARY KEY(id),
CONSTRAINT `FKUSER` FOREIGN KEY (id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=ndb;
CREATE TABLE transcriptionServices (
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(80) NOT NULL,
URL VARCHAR(80) NOT NULL
) ENGINE=ndb AUTO_INCREMENT=0;
ALTER TABLE transcriptionConfig
ADD `serviceName` VARCHAR(80) NOT NULL;
ALTER TABLE transcriptionConfig
ADD CONSTRAINT FK_transcriptionServiceName
FOREIGN KEY (serviceName) REFERENCES transcriptionServices(name)
ON DELETE CASCADE;

Related

what is the problem with my tables in mysql?

I'm going to create 3 table. table "post" and "taxonomy" are connected to "taxonomy_relationship" table with primary key and foreign key but i don't know why do i get this error:
1005 - Can't create table taxonomy_relationship (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE `post`(
id int not null AUTO_INCREMENT,
title varchar(255) not null,
content TEXT not null,
PRIMARY KEY(id)
);
CREATE TABLE `taxonomy`(
id int not null AUTO_INCREMENT,
tax_title varchar(255) not null,
type varchar(255) not null,
PRIMARY KEY(id, tax_title)
);
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
title varchar(255) not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
);
when i use "SHOW ENGINE INNODB STATUS" to show error details it returns:
>
2020-01-19 16:08:31 0x2fb8 Error in foreign key constraint of table blog.taxonomy_relationship:
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to https://mariadb.com/kb/en/library/foreign-keys/ for correct foreign key definition.
Create table blog.taxonomy_relationship with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
)'.
You should to add index KEY title_idx (tax_title) on taxonomy table :
CREATE TABLE `taxonomy` (
`id` int NOT NULL AUTO_INCREMENT,
`tax_title` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `title_idx` (`tax_title`)
);
After you can create foreign key:
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
title varchar(255) not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Another way you can duplicate title in taxonomy_relationship but link it by id field:
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
taxonomy_id int not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (taxonomy_id)
REFERENCES taxonomy(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);

MySQL foreign key constraint error code 1215

Can anyone see the problem with my create statement for table contactgroup. I keep getting error code 1215 can't create foreign key constraint.
create table contact(
ContactID int(5) not null auto_increment,
ContactName varchar(255) Not null,
ContactNumber int(5),
ContactEmail varchar(255),
primary key(ContactID))ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table contactgroup(
ContactGroupID int(5) Not Null,
ContactID int(5) Not Null,
primary key(ContactGroupID),
key fk_contactgroup_ContactID (ContactID),
constraint fk_contactgroup_ContactID
foreign key(ContactID)
references contact) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You need to mention both the table and the primary key column of that table when defining a foreign key constraint:
FOREIGN KEY (ContactID) REFERENCES contact(ContactID)

Error on add foreign key constraint

Every time I'm trying to insert a foreign key to the table I got that message:
Error Code: 1215. Cannot add foreign key constraint 0.281 sec
My create table code:
CREATE TABLE `test`.`buy`(
`id` INT NOT NULL AUTO_INCREMENT,
`id_customer` INT UNSIGNED NOT NULL,
`code` VARCHAR(45) NOT NULL,
PRIMARY KEY(`id`),
UNIQUE INDEX `code_UNIQUE`(`code`),
CONSTRAINT `id_customer` FOREIGN KEY(`id_customer`) REFERENCES `test`.`customer`(`id_customer`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `code` FOREIGN KEY(`code`) REFERENCES `test`.`product`(`code`) ON DELETE RESTRICT ON UPDATE CASCADE
)
What should I do ?
Please format your code next time so it's easier to read.
You don't need the CONSTRAINT xxx bit, try this instead:
CREATE TABLE test.buy (
id INT NOT NULL AUTO_INCREMENT,
id_customer INT UNSIGNED NOT NULL,
code VARCHAR(45) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX code_UNIQUE (code),
FOREIGN KEY (id_customer)
REFERENCES test.customer (id_customer)
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (code)
REFERENCES test.product (code)
ON DELETE RESTRICT ON UPDATE CASCADE
);
If that still doesn't work then make sure the other tables you reference (test.customer and test.product) already exist and that they have matching fields of the same data type.

ERROR 1005 (HY000): Can't create table 'tracker_phonetracker.location_share' (errno: -1)

I am facing this problem in this query .
CREATE TABLE location_share (
circle_id INT ,
user_id INT ,
location_sharing_id INT ,
PRIMARY KEY (user_id ,circle_id ) ,
CONSTRAINT fkcircle1 FOREIGN KEY (circle_id) REFERENCES circle(id) ON DELETE CASCADE ON UPDATE CASCADE ,
CONSTRAINT fkuser1 FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE ON UPDATE CASCADE ,
CONSTRAINT fksharing_policy FOREIGN KEY (location_sharing_id) REFERENCES share(id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB
Mysql shows me error 1005 :
ERROR 1005 (HY000): Can't create table
'tracker_phonetracker.location_share' (errno: -1)
Other tables on which this query is dependent are :
Table : circle
CREATE TABLE circle (
id INT AUTO_INCREMENT PRIMARY KEY ,
name varchar(35)
) ENGINE=INNODB
Table : user
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY ,
contact_no VARCHAR(25) UNIQUE NOT NULL,
email VARCHAR(50) UNIQUE NOT NULL,
first_name VARCHAR(25) NOT NULL ,
last_name VARCHAR(25) ,
device_id VARCHAR(250)NOT NULL ,
image_path VARCHAR(180) ,
password VARCHAR(30) NOT NULL ,
latitude VARCHAR(18) ,
longitude VARCHAR(18)
) ENGINE=INNODB
Table : share
CREATE TABLE share (
id INT AUTO_INCREMENT PRIMARY KEY ,
policy VARCHAR(6) UNIQUE NOT NULL
)
Can anybody plz tell me what's wrong with my query .
Thanks !!
From the manual: http://dev.mysql.com/doc/refman/5.0/en/innodb-error-codes.html
1005 (ER_CANT_CREATE_TABLE)
Cannot create table. If the error message refers to error 150, table
creation failed because a foreign key constraint was not correctly
formed. If the error message refers to error −1, table creation
probably failed because the table includes a column name that matched
the name of an internal InnoDB table.
This is caused by using user you can fix this by escaping references to user:
REFERENCES `user`(id)
CREATE TABLE `location_share` (
`circle_id` int(11) NOT NULL DEFAULT '0',
`user_id` int(11) NOT NULL DEFAULT '0',
`location_sharing_id` int(11) DEFAULT NULL,
PRIMARY KEY (`user_id`,`circle_id`),
KEY `fkcircle1` (`circle_id`),
KEY `fksharing_policy` (`location_sharing_id`),
CONSTRAINT `fkcircle1` FOREIGN KEY (`circle_id`) REFERENCES `circle` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fksharing_policy` FOREIGN KEY (`location_sharing_id`) REFERENCES `share` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fkuser1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;
http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
SOLUTION (works for me):
Rename the table in the script. Ex: wp_users to wp_users1
Run the script. No error.
Rename the table with Operation in MySQL (or your Database Engine).
Ready.
Regards!

multiple foreign keys cannot be added

I am wondering why i cannot be able to add this foreign keys.This is my schema
CREATE TABLE members(
member_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
num_1 int,
num_2 int,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (member_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE contacts(
contact_id INT NOT NULL AUTO_INCREMENT,
s1 int,
phone_number VARCHAR(10) NOT NULL,
s2 int,
s3 int,
PRIMARY KEY (contact_id),
FOREIGN KEY (s1) REFERENCES members(num_1) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s2) REFERENCES members(num_2) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s3) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8
I get this error on the mysql terminal
ERROR 1215 (HY000): Cannot add foreign key constraint
Is there a problem with my schema?.
Works for me this way:
CREATE TABLE members(
member_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
num_1 int,
num_2 int,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (member_id),
key idx_num1 (num_1),
key idx_num2 (num_2)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE contacts(
contact_id INT NOT NULL AUTO_INCREMENT,
s1 int,
phone_number VARCHAR(10) NOT NULL,
s2 int,
s3 int,
PRIMARY KEY (contact_id),
FOREIGN KEY (s1) REFERENCES members(num_1) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s2) REFERENCES members(num_2) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s3) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Just added
key idx_num1 (num_1),
key idx_num2 (num_2)
in table members. Foreign keys need to reference an indexed column (not necessarily unique and not necessarily NOT NULLable).
From the manual:
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.