Foreign key constraint error 1452 in MySQL - Magento import - magento

i am trying to import a sql dump of magento along with some product data and i get this foreign key constraint error:
`ERROR 1452 (23000) at line 231680: Cannot add or update a child row: a foreign key constraint fails:
`magento`.`#sql-b33_27`, CONSTRAINT `FK_CATALOG_COMPARE_ITEM_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE CASCADE ON )`
This is the sql code which is causing the error :
--
-- Constraints for table `catalog_eav_attribute`
--
ALTER TABLE `catalog_eav_attribute`
ADD CONSTRAINT `FK_CATALOG_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID` FOREIGN KEY (`attribute_id`) REFERENCES `eav_attribute` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE;
I am not very comfortable with sql queries. Could some one please explain me what this query does and guide me to resolve this? Thanks.

You are trying to add a record into catalog_eav_attribute, but you do not have a corresponding record in eav_attribute that matches on attribute_id
If you are also inserting bulk data into eav_attribute, I would recommend doing that first, and then the data would be in the table before the foreign key on catalog_eav_attribute needed to reference it.
This article discusses how you can use:
SET FOREIGN_KEY_CHECKS = 0;
--Do your update here
SET FOREIGN_KEY_CHECKS = 1;
If you cannot change the order that you are inserting data. You just have to make sure your data follows the Foreign Keys once everything has been inserted into the database, before you can re-enable the FOREIGN_KEY_CHECKS

I used a database repair tool, after that did this in SQL:
DROP TABLE catalog_product_flat_1 ,
catalog_product_flat_2 ,
catalog_product_flat_3 ;
Now the index is built succesfully.

Related

Goose - Got error when add new line in sql file

Here is the sql statement in migration file
-- +goose Up
-- +goose StatementBegin
ALTER TABLE `companies` ADD CONSTRAINT `fk_company_created_user_id` FOREIGN KEY (`created_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE `companies` ADD CONSTRAINT `fk_company_updated_user_id` FOREIGN KEY (`updated_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
-- +goose StatementEnd
And I got the error below
goose run: ERROR 20221002115116_add_foreign_key_to_company_user.sql:
failed to run SQL migration:
failed to execute SQL query "ALTER TABLE `companies` ADD CONSTRAINT `fk_company_created_user_id` FOREIGN KEY (`created_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;\nALTER TABLE `companies` ADD CONSTRAINT `fk_company_updated_user_id` FOREIGN KEY (`updated_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;\n":
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER TABLE `companies` ADD CONSTRAINT `fk_company_updated_user_id` FOREIGN KEY ' at line 2
What's wrong here?

Oracle - TOM's package for "ON CASCADE UPDATE" [duplicate]

In MS SQL Server it is possible to create a foreign key with ON UPDATE CASCADE option, so whenever you update one of the columns in the primary key, the foreign keys in other tables will also be update by the DBMS.
So, how to do it in Oracle?
Oracle does not allow a Foreign Key constraint with “ON UPDATE CASCADE”.
Here are a couple of options you have.
Create the Foreign Key, and create an “On Update” trigger.
Make use of the package below (needs to be installed in the db).
http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteupdatecascade.html
Let me know if you have additional questions or need more information.
Would a database trigger do the job for you ?
Here is the Oracle doc on the subject of Data Integrity for 11g (just incase you were interested).
You can't use on update cascade, but you can create a trigger that will resolve the issue:
create table tab1(
pk int PRIMARY KEY,
aa int);
create table tab2(
pk int PRIMARY KEY,
tab1_pk int,
FOREIGN KEY(tab1_pk) REFERENCES tab1(pk));
------------------------------------------
create or replace trigger tab1_pkUpdate
after update of pk on tab1
for each row
begin
update tab2 s
set s.tab1_pk = :new.pk
where s.tab1_pk = :old.pk;
end;
/

Magento Product Flat Data Reindex causes error

I cannot reindex the Product flat data because I get the following error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`fpshop/#sql-6101_1484e`, CONSTRAINT `FK_CAT_PRD_FLAT_1_ENTT_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CA)
Any ideas where to look to fix?
Go to your database and check all the rows in table catalog_product_entity or catalog_product_flat_1, particularly the values in column entity_id.
Most likely some rows are missing the required values, which means that either someone has messed with the db, or you are using a module, which is messing up the db. Either you will need to delete those broken rows, or fill them with values they are missing.
fix proposal:
In order to fix this, try to run a query below to locate the broken rows :
SELECT a.entity_id FROM catalog_product_flat_1 AS a LEFT JOIN catalog_product_entity AS b ON a.entity_id = b.entity_id WHERE ISNULL(b.entity_id);
And then delete those rows you got by doing
DELETE FROM catalog_product_flat_1 where entity_id = '123456789';
where 123456789 is the id of the broken row.
Have you tried disabling Flat_catalog_category and Flat_catlaog_product and then truncating the catalog_product_flat tables.Be sure to disable foreign key constraints checks by doing SET FOREIGN_KEY_CHECKS = 0; before truncating. Once doing that you should be able to manually reindex via ssh . A well documented solution for this issue is here : http://binarythoughts21.blogspot.in/2013/12/reindexing-problems-in-magento.html

how to drop and add multi constraints by 1 sql statement for H2 database

I'm working on H2 database, and I meet this problem -
to drop one constraint is fine, I can use this statement
alter table customer drop constraint if exists fk_customer_order ;
for add one constraint is fine too, I can use this statement.
alter table customer add constraint fk_customer_order foreign key (order_id) references order (id) on delete cascade on update cascade;
but the problems is, in customer table I have more foreign key and I want delete them in one query statement.
Something like this
alter table customer drop constraint fk_customer_order
drop constraint fk_customer_information
drop constraint ....
but this seem can not be done in h2 database, anyone can tell me can or not add or drop multi constraint by 1 sql statment? Any answer are welcome and I appreciate much.
I think it can not be done. Why don't you use multiple statements?

How to create a Foreign Key with "ON UPDATE CASCADE" on Oracle?

In MS SQL Server it is possible to create a foreign key with ON UPDATE CASCADE option, so whenever you update one of the columns in the primary key, the foreign keys in other tables will also be update by the DBMS.
So, how to do it in Oracle?
Oracle does not allow a Foreign Key constraint with “ON UPDATE CASCADE”.
Here are a couple of options you have.
Create the Foreign Key, and create an “On Update” trigger.
Make use of the package below (needs to be installed in the db).
http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteupdatecascade.html
Let me know if you have additional questions or need more information.
Would a database trigger do the job for you ?
Here is the Oracle doc on the subject of Data Integrity for 11g (just incase you were interested).
You can't use on update cascade, but you can create a trigger that will resolve the issue:
create table tab1(
pk int PRIMARY KEY,
aa int);
create table tab2(
pk int PRIMARY KEY,
tab1_pk int,
FOREIGN KEY(tab1_pk) REFERENCES tab1(pk));
------------------------------------------
create or replace trigger tab1_pkUpdate
after update of pk on tab1
for each row
begin
update tab2 s
set s.tab1_pk = :new.pk
where s.tab1_pk = :old.pk;
end;
/

Resources