Error (150) Generated By An SQL Script? - mysql

I am trying to create a table on MySQL using an SQL script and the MySQL database keeps on giving me error 150. I've tried the following code in both Navicat and MySQL Workbench and both give an 150 error.
It appears that this is a foreign key problem but i cant actually see what the problem with my code is and was wondering if more expereinced DB users would have any inkling to the problem?
SET FOREIGN_KEY_CHECKS=0;
CREATE TABLE `network_invites` (
`invite_ID` int(11) NOT NULL AUTO_INCREMENT,
`invite_From` int(11) NOT NULL,
`invite_Network` int(11) NOT NULL,
`invite_NetCreator` int(11) NOT NULL,
`invite_Message` varchar(256) NOT NULL,
`invite_Date` date DEFAULT NULL,
PRIMARY KEY (`invite_ID`),
KEY `invite_From` (`invite_From`),
KEY `invite_Network` (`invite_Network`),
KEY `invite_NetCreator` (`invite_NetCreator`),
CONSTRAINT `network_invites_ibfk_1` FOREIGN KEY (`invite_From`) REFERENCES `users` (`user_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `network_invites_ibfk_2` FOREIGN KEY (`invite_Network`) REFERENCES `networks` (`network_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `network_invites_ibfk_3` FOREIGN KEY (`invite_NetCreator`) REFERENCES `networks` (`network_Creator`) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
I cant see what the FK problem is, those fields do exist on the DB and they are of the same datatype.
Thanks for any help.

I think the workaround is :
create the table without constraints
add constraints using ALTER TABLE
again , this is a workaround , and would love to hear from people#mysql or someone more experienced

Make sure the 2 Table Types are both InnoDB. Also make sure your Primary and Foreign keys are all Unsigned.
All these settings can be changed in NaviCat using the "Design Table" option.
I've come across this problem many times, and this does the trick 99% of the time.

You need to have indexes on users.user_ID, networks.network_ID and networks.network_Creator and the types of the columns should be exactly the same as in the network_invites table.

The problem is a typo:
CONSTRAINT `network_invites_ibfk_2`
FOREIGN KEY (`invite_Network`)
REFERENCES `networks` (`network_ID`) ---<--- either here
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `network_invites_ibfk_3`
FOREIGN KEY (`invite_NetCreator`)
REFERENCES `networks` (`network_Creator`) ---<--- or here (more probable)
ON DELETE CASCADE ON UPDATE CASCADE
Your networks table Primary Key is probably network_ID and not network_Creator.

Related

MySQL fails because of FK constraint that should not exist

I'm running the following very boring query:
alter table mytable change user_id user_id varchar(36) null default null,
add foreign key (user_id) references users(id) on delete set null;
But it fails with the following error:
#1452 - Cannot add or update a child row: a foreign key constraint fails
(`mydatabase`.`#sql-602_60f`, CONSTRAINT `#sql-602_60f_ibfk_4`
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
The problem is that that foreign key constraint doesn't exist, so I can't delete it. There are 3 foreign key constaints on the table (mytable_ibfk_1, mytable_ibfk_2, mytable_ibfk_3), but nothing with the name mentioned, and trying to delete it produces another error.
Any ideas how to fix this?
As Michael Berkowski pointed out, I was misreading the error, which was pointing to conflicting values in the two columns, not to an existing phantom constraint that I couldn't delete.
The root of the problem was that the column had previously accepted empty string values (''), and those needed to be converted to NULL in order for the foreign key constraint to be put in place.

Cannot delete a row because of foreign key constraint

I have two tables. user and comment. I wanted the users to be able to vote up comments so I made another table called user_vote_comment. I let the id column on the user table and the id column on the comment table be a composite primary key on the user_vote_comment table.
This is the structure of the user_vote_comment table:
CREATE TABLE IF NOT EXISTS `user_vote_comment` (
`user_id` INT(11) UNSIGNED NOT NULL,
`comment_id` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`user_id`, `comment_id`),
INDEX `fk_user_vote_comment_comment1_idx` (`comment_id` ASC),
CONSTRAINT `fk_user_vote_comment_user1`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_user_vote_comment_comment1`
FOREIGN KEY (`comment_id`)
REFERENCES `comment` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
Now I added some rows to the user_vote_comment but then I wanted to delete some votes because I wanted the users to have that option, to unvote. But then I get this error message:
Error 1451: Cannot delete or update a parent row: a foreign key constraint fails (`user_vote_comment`, CONSTRAINT `fk_user_vote_comment_comment1` FOREIGN KEY (`comment_id`) REFERENCES `comment` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I thought the user and comment table were the parent tables here? Since it makes sense that it is a constraint if I was to delete a user or a comment then I should get an error because some rows in the user_vote_comment are dependent on that info, but not the other way around?
I don't understand why I can't delete the rows in the user_vote_comment, I feel that I am too much restricted if I can't even remove rows from it.
What should I do?
I don't think anything is wrong with your SQL. May be you've missed mentioning something else in the above problem which is causing the issue?
I did a quick test with SQLFiddle (http://www.sqlfiddle.com/#!9/c726aa/1/0) and I'm able to delete from USER_VOTE_COMMENT table just fine.
You can set ON DELETE CASCADE instead of ON DELETE NO ACTION

Changing MYSQL server to match new rails application version KEY, CONSTRAINT, and COLUMN

SETTING
I have a rails application that references an external mysql server. I am about to push some changes to the rails application. I made some changes to my local/test version of the application's database using rails migrations, but my production/live version of the application's database is not on a server that has the rails application, so I don't think I can just run the rails migration on the mysql database.
QUESTION
How do I safely add the following column, key and constraint:
`item_id` int(11) DEFAULT NULL,
KEY `index_notifications_on_item_id` (`item_id`),
CONSTRAINT `fk_rails_f395ae520f` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE,
and safely change the unique key:
UNIQUE KEY `item_id_and_user_id_stand_id_unique_index` (`item_id`,`user_id`,`stand_id`)
to
UNIQUE KEY `item_id_and_user_id_stand_id_notified_user_id_unique_index` (`item_id`,`user_id`,`stand_id`,`notified_user_id`),`)
ATTEMPTS
CONCERNING THE CONSTRAINT AND KEY
I try running:
ALTER TABLE notifications ADD FOREIGN KEY fk_rails_f395ae520f(item_id) REFERENCES items(id) ON DELETE CASCADE;
But, I run into 2 issues:
1: It names the key what I want the constraint to be named and makes up its own constraint name.
OUTPUT:
KEY `fk_rails_f395ae520f` (`item_id`),
CONSTRAINT `notifications_ibfk_2` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE,
It requires me to temporarily set: set foreign_key_checks = 0; or else I get the error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (DB_NAME.#sql-3c9_49, CONSTRAINT #sql-3c9_49_ibfk_2 FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE)
and I do not know if doing that is "safe" to do on a production/live server.
CONCERNING THE COLUMN AND UNIQE KEY
I believe I have the column and unique keys safely added with:
ALTER TABLE notifications add COLUMN item_id INT(11) DEFAULT NULL;
ALTER TABLE notifications DROP INDEX index_notifications_on_user_id_and_notified_user_id, ADD UNIQUE KEY `item_id_and_user_id_stand_id_notified_user_id_unique_index` (stand_id, user_id, notified_user_id, item_id);
CLARIFICATION
The reason I bring up the column and unique key even though I may have it working is the same reason I gave a setting: I am new to this and may be taking a flawed approach to the whole scenario. I am hoping this context allows someone to call me out on it if that's the case.
UPDATE 1
ALTER TABLE notifications ADD CONSTRAINT fk_rails_f395ae520f FOREIGN KEY index_notifications_on_item_id(item_id) REFERENCES items(id) ON DELETE CASCADE;
outputs:
CONSTRAINT `fk_rails_f395ae520f` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE,
Which is what I want, but I am still working on the KEY section.
UPDATE 2
ALTER TABLE notifications add COLUMN item_id INT(11) DEFAULT NULL;
ALTER TABLE notifications DROP INDEX index_notifications_on_user_id_and_notified_user_id, ADD UNIQUE KEY `item_id_and_user_id_stand_id_notified_user_id_unique_index` (stand_id, user_id, notified_user_id, item_id);
CREATE INDEX index_notifications_on_item_id ON notifications (item_id);
ALTER TABLE notifications ADD CONSTRAINT fk_rails_f395ae520f FOREIGN KEY index_notifications_on_item_id(item_id) REFERENCES items(id) ON DELETE CASCADE;
I believe these four lines in sequence answers the question of how to manually handle what my rails migration does. But, I am going to leave this question here. The question that remains is: "is it a terrible idea to do what I just did? and if so, how to do proceed?"

ERROR 1452: Cannot add or update a child row: a foreign key constraint fails

I have created two tables in MySQL 5.6.11 as shown below by means of MySQL Workbench 5.2.47.
The country table:
delimiter $$
CREATE TABLE `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INC
REMENT=2 DEFAULT CHARSET=utf8$$
The state_table:
delimiter $$
CREATE TABLE `state_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_name` varchar(45) DEFAULT NULL,
`country_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `country_fk` FOREIGN KEY (`id`) REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT=''$$
There is one row in the country table with the id 1. It allows only one (child) row to be inserted into its child table state_table. If more rows are attempted, then the following error occurs.
ERROR 1452: Cannot add or update a child row: a foreign key constraint
fails (social_networking.state_table, CONSTRAINT country_fk
FOREIGN KEY (id) REFERENCES country (id) ON DELETE CASCADE ON
UPDATE CASCADE)
SQL Statement:
INSERT INTO `social_networking`.`state_table` (`id`, `state_name`, `country_id`) VALUES ('2', 'xxx', '1')
Actually, I'm trying to map these tables using an ORM (JPA) where I always see only OneToOne relationship.
What am I missing?
Well, I find the answer, the solution, my english is not very well but I think can explain you. I get this error after try to create a trigger, My database was created in phpmyadmin, and this error was make me crazy, the problem was that I before create the trigger, I load a lot of data in my tables, and in my child table was some data that have no match in my parent table, ej: my child table "chat" have a "id_jugador=1" and in my parent table there wasn't that "id_jugador", that was my mistake, I hope help you, Argentina Rulz ;)
I think you have a typo in your foreign key constraint, country_id should probaby be the foreign key to country. When id is the foreign key, you can only insert one row since it just happens to get id=1 which is the same id as the row in country;
CONSTRAINT `country_fk` FOREIGN KEY (`id`)
REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
should probably be
CONSTRAINT `country_fk` FOREIGN KEY (`country_id`)
REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
An SQLfiddle to test with.
I had the same problem and it was not wrong relationships name.
I had problem with different records, ie, tried registering a record that did not exist in another table so generated this error.
Check if the record exists in another table to insert their correct relationship, otherwise, this error appears.
Hope this help you.
example:
table1(
id1 INT PRIMARY KEY,
name1 VARCHAR(50)
)
table2(
id2,<--- want to add FOREIGN KEY to this field
name2 VARCHAR(50)
)
Before adding constraint foreign key you must have the right value between id1 and id2, so you should update that id field with the value that map each other.
Possible Solution
if you have live data, then check all column values.
i.e. if you have 'x'->table as primary one having 'a'->column and 'y'->table as secondary with 'b'->column, then all values in 'b'->column must exist in 'a'->column if any value that exists in 'b'->column and not exist in 'a'->column then it will give as such error..
Hope this help.. for newbie..

MySQL -- create two indexes, only one appears in SHOW CREATE TABLE output

In have a many-to-many linking table and I'm trying to set up two foreign keys on it. I run these two statements:
ALTER TABLE address_list_memberships
ADD CONSTRAINT fk_address_list_memberships_address_id
FOREIGN KEY index_address_id (address_id)
REFERENCES addresses (id);
ALTER TABLE address_list_memberships
ADD CONSTRAINT fk_address_list_memberships_list_id
FOREIGN KEY index_list_id (list_id)
REFERENCES lists (id);
I would expect that when I run SHOW CREATE TABLE address_list_memberships I'd see this:
[...]
KEY `index_address_id` (`address_id`),
KEY `index_list_id` (`list_id`),
CONSTRAINT `fk_address_list_memberships_list_id` FOREIGN KEY (`list_id`)
REFERENCES `lists` (`id`),
CONSTRAINT `fk_address_list_memberships_address_id` FOREIGN KEY (`address_id`)
REFERENCES `addresses` (`id`)
But instead I get this:
[...]
KEY `index_list_id` (`list_id`),
CONSTRAINT `fk_address_list_memberships_list_id` FOREIGN KEY (`list_id`)
REFERENCES `lists` (`id`),
CONSTRAINT `fk_address_list_memberships_address_id` FOREIGN KEY (`address_id`)
REFERENCES `addresses` (`id`)
It looks as though only one index is there. Seems to contradict the MySQL docs which say MySQL automatically creates an index on the referencing column whenever you create a foreign key.
I've noticed this only-one-index thing every time I create two FKs on a table whether I use a GUI tool such as CocoaMySQL or SQLyog, or whether I do it on the command line.
Any illumination of this mystery would be very much appreciated.
I just tried it and it works fine for me. I copied and pasted the ALTER statements you wrote and here is what I get:
mysql> show create table address_list_memberships;
CREATE TABLE `address_list_memberships` (
`address_id` bigint(20) unsigned NOT NULL,
`list_id` bigint(20) unsigned NOT NULL,
KEY `index_address_id` (`address_id`),
KEY `index_list_id` (`list_id`),
CONSTRAINT `fk_address_list_memberships_list_id`
FOREIGN KEY (`list_id`) REFERENCES `lists` (`id`),
CONSTRAINT `fk_address_list_memberships_address_id`
FOREIGN KEY (`address_id`) REFERENCES `addresses` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I'm using MySQL 5.0.51a on Mac OS X.
edit: Try the following query to get all the indexes MySQL thinks exist on your table:
SELECT * FROM information_schema.key_column_usage
WHERE table_schema = 'test' AND table_name = 'address_list_memberships'\G
(I used the 'test' database for my test; you should replace this string with the name of the schema where your table is defined.)
It doesn't really matter. You still have an index on list_id. MySQL requires any foreign key constraint to also have an index on the referencing fields. Since both index_list_id and fk_address_list_memberships_list_id are built on list_id, MySQL probably sees this and uses index_list_id as the index, renaming it to fk_address_list_memberships_list_id. You could even skip declaring the index, since MySQL will do it implicitly in the version you are using.