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?
Related
I tried the following query to delete some rows from the SQL database but it's showing following error:
Query:
Delete From Document_Type where Doc_Type_id='case'
This is the error:
Error starting at line : 4 in command -
delete From Document_Type where Doc_Type_Desc='Case'
Error report -
SQL Error: ORA-02292: integrity constraint (NW_DEV_281015.FK_COMMENTS_DOCUMENT_TYPE6) violated - child record found
02292. 00000 - "integrity constraint (%s.%s) violated - child record found"
*Cause: attempted to delete a parent key value that had a foreign dependency.
*Action: delete dependencies first then parent or disable constraint.
How to solve this problem
I assume Oracle by the error.
Like the error message said, you have a constraint that forces the child table to be connected. So first, delete the same records from the child table:
DELETE FROM Child_Table t
WHERE t.<FK> IN(SELECT s.PK FROM Document_Type s where s.Doc_Type_Desc='Case')
Then do you delete. Or alternativly , disable the constraint :
alter table
table_name
DISABLE constraint
constraint_name;
This is my exact query which is erroring out
alter table INDIL_MCAR drop constraint ABOB.INDI_MCAR_PK;
I m trying to remove the unique key constring from the table. It gives me the following error.
ORA-01735: invalid ALTER TABLE option
You can't prefix the constraint name... the table name yes, but not the constraint name. Remove the ABOB.:
alter table INDIL_MCAR drop constraint INDI_MCAR_PK;
Lately I have been trying to add the following foreign key in the table, with the RESTRICT Clause in Oracle with the following command.:
ALTER TABLE
Employee_SalHead
ADD CONSTRAINT PAYROLL_SHEAD_FKEY FOREIGN KEY
(
SalHead_ID
)
REFERENCES SalHead
(
SalHead_ID
)
ON DELETE RESTRICT ENABLE;
This gave me the following error:
Error starting at line : 11 in command - ALTER TABLE Employee_SalHead ADD
CONSTRAINT PAYROLL_SHEAD_FKEY FOREIGN KEY ( SalHead_ID )
REFERENCES SalHead ( SalHead_ID ) ON DELETE RESTRICT ENABLE
Error report - SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Also if I try the same through Oracle SQL developer, I get only the options Set Null, Cascade and No Action Only.
Oracle only supports ON DELETE SET NULL and ON DELETE CASCADE. You can achieve your requirement by simply doing the below query. No need to mention ON DELETE RESTRICT
ALTER TABLE Employee_SalHead
ADD CONSTRAINT PAYROLL_SHEAD_FKEY FOREIGN KEY(SalHead_ID)
REFERENCES SalHead(SalHead_ID);
ON DELETE NO ACTION is Default.
From Documentation
The No Action (default) option specifies that referenced key values cannot be updated or deleted if the resulting data would violate a referential integrity constraint. For example, if a primary key value is referenced by a value in the foreign key, then the referenced primary key value cannot be deleted because of the dependent data.
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.
ALTER TABLE employees
ADD CONSTRAINT emp_dno_fk FORIEGN KEY(Dno) REFERENCES Departments(Dno);
When I use this command it shows an error like
CONSTRAINT Specification not allowed here.
But when I use this command it works:
ALTER TABLE employees
ADD CONSTRAINT emp_dno_fk Dno REFERENCES Departments(Dno);
Can anyone tell me why Oracle doesn't allow FOREIGN KEY KEYWORD in the first command?
The error message, admittedly, is not very helpful. The following are examples of how a referential integrity constraint may be created in Oracle:
The following examples assume that the column Dno already exists in employees:
ALTER TABLE employees ADD
CONSTRAINT emp_dno_fk FOREIGN KEY (Dno) REFERENCES Departments (Dno);
ALTER TABLE employees ADD
FOREIGN KEY (Dno) REFERENCES Departments (Dno);
The following examples assume that the column Dno does not already exist in employees:
ALTER TABLE employees ADD
CONSTRAINT emp_dno_fk Dno REFERENCES Departments (Dno);
ALTER TABLE employees ADD
Dno REFERENCES Departments (Dno);
Personally, I avoid the syntax versions which add the column and prefer to add it myself.