Two tables with bidirectional relationships
Hello,
i am new two MySQL and i am trying to setup my first project which is based on an existing DB (non MySQL).
Now i face a problem.
I have two tables: Departments and Persons. Both tables have unique primary keys.
Each department has a person as head of the department. 1:N This relationship formed no problem.
The second relationship says that every person can belong to a department. But this is not mandatory. So this relationship is 1:N but in the opposite direction.
When i try to send my model to the DB i get a nice little error message (as many i suppose):
Executing SQL script in server
ERROR: Error 6125: Failed to add the foreign key constraint. Missing unique key for constraint 'fk_tbl_persons_tbl_departments1' in the referenced table 'tbl_departments'
SQL Code:
ALTER TABLE `mydb`.`tbl_persons`
ADD CONSTRAINT `fk_tbl_persons_tbl_addresses1`
FOREIGN KEY (`per_adr_index`)
REFERENCES `mydb`.`tbl_addresses` (`adr_index`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_tbl_persons_tbl_departments1`
FOREIGN KEY (`per_dep_index`)
REFERENCES `mydb`.`tbl_departments` (`dep_index`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I am not sure if i am doing something here which is not possible or what the error might be or how i have to deal with this situation.
Subject
Written By
Posted
Two tables with bidirectional relationships
April 18, 2026 10:42AM
Sorry, only registered users may post in this forum.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.