Question

I've got this

CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages` (
  `id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
  `subject` VARCHAR(200) NOT NULL ,
  `body` TEXT NOT NULL ,
  `date` DATETIME NOT NULL ,
  PRIMARY KEY (`id_msg`) ,
  INDEX fk_msg_messages_user (`id_user_from` ASC) ,
  INDEX fk_msg_messages_msg_messages_users (`id_msg` ASC) ,
  CONSTRAINT `fk_msg_messages_user`
    FOREIGN KEY (`id_user_from` )
    REFERENCES `beta`.`user` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_msg_messages_msg_messages_users`
    FOREIGN KEY (`id_msg` )
    REFERENCES `beta`.`msg_messages_users` (`id_msg` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB

*And this is my error. These are my user and msg_messages_users tables too. I've reviewed all my tables and i cant see my error. Im working on MysqlWorbench and it generate this erroneous sintax.*

Error 1005: Can't create table 'beta.msg_messages' (errno: 150).

CREATE  TABLE IF NOT EXISTS `beta`.`user` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `username` VARCHAR(100) NOT NULL ,
   PRIMARY KEY (`id`) ,
ENGINE = InnoDB
AUTO_INCREMENT = 87
DEFAULT CHARACTER SET = utf8;

 CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
      `id` BIGINT UNSIGNED NOT NULL ,
      `id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
      `id_msg` BIGINT(20) UNSIGNED NOT NULL ,
      `status` TINYINT NOT NULL DEFAULT 0 ,
      `date` DATETIME NOT NULL ,
      PRIMARY KEY (`id`) ,
      INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
      CONSTRAINT `fk_msg_messages_users_user`
        FOREIGN KEY (`id_usr_to` )
        REFERENCES `beta`.`user` (`id` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB;
Was it helpful?

Solution

Im a dumb... i was declaring a foreign key in the wrong table..

CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages` (
  `id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
  `subject` VARCHAR(200) NOT NULL ,
  `body` TEXT NOT NULL ,
  `date` DATETIME NOT NULL ,
  PRIMARY KEY (`id_msg`) ,
  INDEX fk_msg_messages_user (`id_user_from` ASC) ,
  CONSTRAINT `fk_msg_messages_user`
    FOREIGN KEY (`id_user_from` )
    REFERENCES `beta`.`user` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
  `id_msg` BIGINT(20) UNSIGNED NOT NULL ,
  `id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
  `status` TINYINT NOT NULL DEFAULT 0 ,
  `date` DATETIME NOT NULL ,
  INDEX id_msg (`id_msg` ASC) ,
  INDEX fk_msg_messages_users_msg_messages (`id_msg` ASC) ,
  INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
  CONSTRAINT `fk_msg_messages_users_msg_messages`
    FOREIGN KEY (`id_msg` )
    REFERENCES `beta`.`msg_messages` (`id_msg` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_msg_messages_users_user`
    FOREIGN KEY (`id_usr_to` )
    REFERENCES `beta`.`user` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

thanks all

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top