Parent table cannot be updated when foreign key is present - mysql

There are two tables one is a parent i.e., groups table which has foreign key to a child table i.e., users. I am not able to edit foreign key column in parent table where as I have given it to cascade to child table. It gives a error as follows:
Error Code : 1452
Cannot add or update a child row: a foreign key constraint fails (`tms`.`groups`, CONSTRAINT `FK_groups` FOREIGN KEY (`GroupName`) REFERENCES `users` (`groupname`) ON DELETE CASCADE ON UPDATE CASCADE)
Thanks,
-Jeevan

I assume a group contains many users, and a users belongs to one group.
Then you have declared the foreign key in the wrong direction. Actually users.groupname must reference tms.groups. Drop the current foreign key and rebuild it the other way around (in the users table).

This happens if you try to reference a non existing entry in database. In short, you inserted into groups and tried to reference an user entry which doesn't exist yet.

Related

What is meant by Error 1451 in MySQL database?

I simply ran the delete query in my sql-yog and i was carried away by this error.
Can anyone give me a explanation for my error.
Cannot delete or update a parent row: a foreign key constraint fails (`db_lakshyaassets3`.`lss_entity`, CONSTRAINT `FK_lss_entity_aid` FOREIGN KEY (`address_id`) REFERENCES `lss_address` (`address_id`))
One of the properties of FOREIGN KEY constraint is that it is used to prevent actions that would destroy links between tables. Therefore you cannot delete rows in a table which shares a FOREIGN KEY constraints with another table without deleting in parent first.
You can handle this in 2 ways:
Use Foreign key with ON DELETE CASCADE (which will delete child rows if parent is deleted) reference
Use Foreign key with ON DELETE NO ACTION (which deletes the parent without exceptions, but your data will become meaningless)
You are getting this error because default property is ON DELETE RESTRICT
HTH
There is foreign key that points to that table....that is the reason you cannot delete it.
You have to delete first the row in the child table...the child table is the referencing table.The foreign key is contained in the child table
You can do this by removing the foreign key constraint check the following link
http://www.justin-cook.com/wp/2006/05/09/how-to-remove-foreign-keys-in-mysql/
Once you remove and delete the data then you can again add the foreign key constraint. Use alter query from the following link
http://www.w3schools.com/sql/sql_foreignkey.asp

child tables and foreign keys

I know there have been myriads of questions concerning primary and foreign keys. Looking through them, I cannot seem to find a simple answer to my question. My understanding of primary and foreign keys is that a foreign key is a column designated in a child table that refers back to a primary key as a column in a parent table. Is that correct, or do I have it backwards? If that is indeed correct, I am trying to find out why I am having difficulty creating a foreign key in a child table as such:
salesorders.sonumber (pk) < customer.sonumber (fk)
I am using Navicat with MariaDB (same as MySQL) and the error I get is:
1452 - Cannot add or update a child row: a foreign key constraint fails ('customer_orders','#sql7a8_3'; CONSTRAINT 'sonumber' FOREIGN KEY ('sonumber') REFERENCES 'salesorder' ('sonumber') ON DELETE NO ACTION ON UPDATE CASCADE)
Customer_orders is the database name. I am naming the foreign key 'sonumber' which is the same as the column name in the child table (customer) and the parent table (salesorders). Is that incorrect? Should I give the foreign key another name?
gitpicker
The foreign key should have its own unique name, yes. sonumber_fk or something similar is a simple naming schema.
You also need to make sure that every entry in the Foreign Key column has a corresponding entry in the Referenced table, otherwise you will not be able to create the foreign key.

I get Cannot add or update a child row: a foreign key constraint fails error

I have a table where I keep
id|user_id|subject_id
I have another two table users and subjects.
user_id is a foriegn key and refer the id in users table id column.
I use php admin and I could create the relation.
Same way, I tried to create relation for the subject_id foriegn key.
But I get the following error.
#1452 - Cannot add or update a child row: a foreign key constraint fails (`version2`.<result 2 when explaining filename '#sql-25b4_1e1'>, CONSTRAINT `#sql-25b4_1e1_ibfk_1` FOREIGN KEY (`id`) REFERENCES `wp_cons_table` (`subject_id`))
all tables are ino db and columns have int(5) data type.
I don't know why I get the error.
Can someone figure the reson to this error.
The specific link it's failing on is described at the end of your error:
FOREIGN KEY (`id`) REFERENCES `wp_cons_table` (`subject_id`)
It would be useful to have clearer information about the tables but essentially there are values already in your child table that do not exist in the parent table.
If there is any data that would violate the constraint then you won't be allowed to create it. Delete the mismatched child data or create the parents and you should be fine.
See also: alter table add foreign key fails

Delete a Primary Key Without Affecting Foreign Key Constrain to Other Table

I want to delete a record from a school table without affecting a foreign key to the department name. I tried but I got this message:
"Cannot delete or update a parent row: a foreign key constraint fails
(arusms.department, CONSTRAINT department_ibfk_1 FOREIGN KEY
(school_name) REFERENCES school (school_name) ON UPDATE
CASCADE)"
I'm not sure why you would want to do that. If you delete the school, the department will be orphaned. That's the point of having foreign keys in the first place, to enforce referential integrity. If you want the department to remain and to be able to do this, you will need to alter the foreign key to include ON DELETE SET NULL. Otherwise, you will have to drop the constraint, perform the delete, and recreate the constraint.
Your error message is hiding the real cause.
(
arusms.department,
CONSTRAINT department_ibfk_1
FOREIGN KEY (school_name)
REFERENCES school (school_name)
ON UPDATE CASCADE
)
When you created the foreign key constarint, you omitted the ON DELETE part. MySQL used the default action for this, which is ON DELETE RESTRICT. See the MySQL docs: FOREIGN KEY Constraints
If you want to be able to delete schools without cascading effect to the related departments, you can either
remove the FK constraint or
make the column (department.school_name) nullable and alter the constraint to have the ON DELETE SET NULL action.
If you want to be able to delete schools and cascading deleting the related departments, you can
alter the constraint to have the ON DELETE CASCADE action.
The whole purpose of having a foreign key is to keep data consistent. In your case, it means that for each department, there must exist a corresponding school record. And if you DELETE a school, all corresponding departments should be deleted as well, or at least their school reference must be NULLed.
If you don't need this kind of enforcement, DROP the foreign key.
Alternatively, if you just want to reassign a department to another school, first do that, and only then DELETE the original school.

MySQL Error, what does it mean

I am trying to update two columns in a xref database. I am getting this error message:
Cannot add or update a child row: a foreign key constraint fails (`globaldetroit`.`org_cult_xref`, CONSTRAINT `org_cult_xref_ibfk_1` FOREIGN KEY (`org_id`) REFERENCES `organization` (`org_id`) ON DELETE CASCADE ON UPDATE CASCADE)
I want to be able to have a many-many relationship, and these errors seem to prevent me from having one.
EDIT:
That is very odd! There most certainly is a column org_id with the value of "6" (as an integer) in the table organization! I just checked!
You are trying to set a value that has a foreign key constrant -- ie the key does not exist in the foreign table.
So globaldetroit's org_cult_xref references an org_id in organization that does not exist.
you're putting in field globaldetroit.org_cult_xref value not existing in organization.org_id
Many-many relationship is bad, don't go there.
Your error seems to be caused because the row you insert in org_cult_xref has a column org_id and the value you insert there cannot be found in the organization table.
You have a foreign key defined on the table you are trying to insert/update into, which basically says the value in org_id should exist in the organization table, and it is not the case.