Foro de elhacker.net

Programación => Bases de Datos => Mensaje iniciado por: Proteus1989 en 29 Diciembre 2012, 04:28 am



Título: Problema con las claves foraneas (errno 150)
Publicado por: Proteus1989 en 29 Diciembre 2012, 04:28 am
Os pongo directamente el código a ver si conseguís dar con el error, porque yo no lo encuentro por ningún lado.
El error me da al crear la tabla privates_zones

Código
  1. -- -----------------------------------------------------
  2. -- Table `mydb`.`guest_zones`
  3. -- -----------------------------------------------------
  4. CREATE  TABLE IF NOT EXISTS `mydb`.`guest_zones` (
  5.  `player_name` VARCHAR(45) NOT NULL ,
  6.  `zone_name` VARCHAR(45) NOT NULL ,
  7.  PRIMARY KEY (`player_name`, `zone_name`) )
  8. ENGINE = InnoDB;
  9.  
  10.  
  11. -- -----------------------------------------------------
  12. -- Table `mydb`.`privates_zones`
  13. -- -----------------------------------------------------
  14. CREATE  TABLE IF NOT EXISTS `mydb`.`privates_zones` (
  15.  `zone_name` VARCHAR(45) NOT NULL ,
  16.  `player_name` VARCHAR(45) NOT NULL ,
  17.  `price` INT NULL ,
  18.  PRIMARY KEY (`zone_name`) ,
  19.  CONSTRAINT `fk_privates_zones_guest_zones1`
  20.    FOREIGN KEY (`zone_name` )
  21.    REFERENCES `mydb`.`guest_zones` (`zone_name` )
  22.    ON DELETE CASCADE
  23.    ON UPDATE CASCADE)
  24. ENGINE = InnoDB;
  25.  
  26. -- -----------------------------------------------------
  27. -- Table `mydb`.`users`
  28. -- -----------------------------------------------------
  29. CREATE  TABLE IF NOT EXISTS `mydb`.`users` (
  30.  `player_name` VARCHAR(45) NOT NULL ,
  31.  `vip` TINYINT(1) NULL ,
  32.  PRIMARY KEY (`player_name`) ,
  33.    CONSTRAINT `fk_users_privates_zones1`
  34.    FOREIGN KEY (`player_name` )
  35.    REFERENCES `mydb`.`privates_zones` (`player_name` )
  36.    ON DELETE CASCADE
  37.    ON UPDATE CASCADE)
  38. ENGINE = InnoDB;
  39.  

Espero sepáis resolverme la duda.


Título: Re: Problema con las claves foraneas (errno 150)
Publicado por: RevangelyonX en 30 Diciembre 2012, 23:51 pm
Hola,

Al hacer referencia en la segunda tabla al primary key de la primera ya no da error:

Código:
-- -----------------------------------------------------
-- Table `mydb`.`privates_zones`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`privates_zones` (
 `zone_name` VARCHAR(45) NOT NULL ,
 `player_name` VARCHAR(45) NOT NULL ,
 `price` INT NULL ,
 PRIMARY KEY (`zone_name`) ,
 CONSTRAINT `fk_privates_zones_guest_zones1`
   FOREIGN KEY (`player_name`, `zone_name` )
  REFERENCES `mydb`.`guest_zones` (`player_name`,`zone_name` )
   ON DELETE CASCADE
   ON UPDATE CASCADE)
ENGINE = InnoDB;

De todos modos, ejecuta la siguiente sentencia desde la consola de MySQL (suponiendo que tengas privilegios):

Código:
> SHOW ENGINE INNODB STATUS

Donde verás errores como el siguiente:

Código:
LATEST FOREIGN KEY ERROR
...
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

Luego en el siguiente enlace tienes posibles causas y solucions del error:

www.eliacom.com/mysql-gui-wp-errno-150.php

Salu2