0

An issue was created about 6 months ago for Laravel Database Seeding. The issue was about how MySQL prevented the truncation of a table due to its foreign key constraints.

Tayor Otwell (the creator of Laravel) stated the following in his comment

https://github.com/laravel/framework/issues/243#issuecomment-13051091

Fixed. Not sure why people still insist on using foreign keys with seeding though. It's nothing but a headache, which is why Rails doesn't even support it.

Please explain how would I go about not using foreign keys with seeding.

An example table structure:

CREATE  TABLE `posts` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(45) NOT NULL,
  `body` TEXT NULL,
  PRIMARY KEY (`id`)
);

CREATE  TABLE `comments` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `comment` VARCHAR(500) NOT NULL ,
  `post_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_comments_1_idx` (`post_id` ASC) ,
  CONSTRAINT `fk_comments_1`
      FOREIGN KEY (`post_id` )
        REFERENCES `blog_test`.`posts` (`id` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
);

Thanks.

4

0 回答 0