Mysql: using two foreign keys to the same table

pixeline

I'm using MySQL workbench to design a database. Server is mysql 5.5.6

I've defined a few foreign keys linking the "candidates" table to the "countries" table. I get this error:

Executing SQL script in server
ERROR: Error 1005: Can't create table 'customer.candidats' (errno: 150)

The thing is: i'm referencing twice the countries table: once for the "nationality" column, once for the user address's country of origin. Is that allowed? Is this the right way to do it?

Here is the generated code that seems to trigger the issue.

CREATE TABLE IF NOT EXISTS `customer`.`candidats` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `nom` VARCHAR(40) NULL,
  `prenom` VARCHAR(40) NULL,
  `qualite` ENUM('0001','0002') NULL COMMENT '0001 = Madame\n0002 = Monsieur',
  `sexe` SET('1','2') NULL COMMENT '1 = Femme\n2 = Homme',
  `date_de_naissance` DATE NULL,
  `Nationalite` INT NOT NULL,
  `selor_bilinguisme` TINYINT(1) NULL,
  `rue` VARCHAR(60) NULL,
  `numero` VARCHAR(10) NULL,
  `pays` INT NOT NULL,
  `region` INT NOT NULL,
  `localité` VARCHAR(40) NULL,
  `code_postal` VARCHAR(10) NULL,
  `email` VARCHAR(241) NULL,
  `tel_domicile` VARCHAR(30) NULL,
  `tel_bureau` VARCHAR(30) NULL,
  `tel_mobile` VARCHAR(30) NULL,
  `tel_prefere` ENUM('01','02','03') NULL DEFAULT '03',
  PRIMARY KEY (`id`),
  INDEX `fk_candidats_pays_idx` (`Nationalite` ASC, `pays` ASC),
  INDEX `fk_candidats_régions1_idx` (`region` ASC),
  CONSTRAINT `fk_candidats_pays`
    FOREIGN KEY (`Nationalite` , `pays`)
    REFERENCES `customer`.`pays` (`id` , `id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_candidats_régions1`
    FOREIGN KEY (`region`)
    REFERENCES `customer`.`régions` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

The "pays" table ("countries" in French)

CREATE TABLE IF NOT EXISTS `customer`.`pays` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `nom_fr` VARCHAR(45) NULL,
  `nom_nl` VARCHAR(45) NULL,
  `nationalite_fr` VARCHAR(45) NULL,
  `nationalite_nl` VARCHAR(45) NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB
hgoebl

Your schema generator doesn't work correctly.

It should generate:

CONSTRAINT `fk_candidats_pays`
    FOREIGN KEY (`pays`)
    REFERENCES `customer`.`pays` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_Nationalite`
    FOREIGN KEY (`Nationalite`)
    REFERENCES `customer`.`pays` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,

To your other question: This type of referencing seems strange when you see it the first time, but it's quite normal and I think there is no other way of constructing this type of relationship.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MYSQL: select by two foreign keys from the same table

mysql queries on two foreign keys referencing the same table

Two foreign keys pointing to the same table / model

JPA Hibernate two foreign keys to the same table

MySQL Using Same Foreign key for two different table columns

(MySQL) 2 foreign keys referencing the same table

MySQL database with two foreign keys in one table

sql for a table with two foreign keys to same table causing Cartesian product

Table with two foreign keys pointing to the same column of another table

MySQL two foreign key from same table

EFCore Linq ThenInclude Two Foreign Keys To Same Table

Eloquent relation setup, two foreign keys to same table laravel 5.2

Entity Framework Code First - two Foreign Keys from same table

SQL query with two columns as foreign keys of the same table

join on two foreign keys from same table in SQL

EF 6 how to set two foreign keys to same table

Entity Framework Core Two Foreign Keys - Same Table

Select name of two foreign keys referring to same primary key table

EF Core 2.2 - Two foreign keys to same table

two foreign keys on same table to display parent fields

Two foreign keys on same table: how to implement on delete cascade?

How to create two foreign keys to the same table with SQLite in Xamari Form

How to add two foreign keys in the same class (table) in EF

two foreign keys to same primary key select statement MYSQL

MySQL table with "calculated values" and keys that are primary and foreign at the same time

Join on two of the same foreign keys

SQL DDL: Recursive table with two foreign keys (MySQL)

How do I programmatically create two foreign keys in mysql table

SQLAlchemy table defining relationship using two foreign keys