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.

erj1
  • 79
  • 3
  • By 'without foreign key' you should understand 'to not specify foreign keys'. In your structure you can create posts *without specifying* a foreign key (it will be autogenerated), then after creating post just create comments associating the posts previously created. I do a lot of seeding and migration, and I can't see your problem. – Rubens Mariuzzo Aug 05 '13 at 18:54
  • Thanks for responding -- So you are saying that I should create the comments table with the column "post_id" but not explicitly create the foreign keys pointing to the post table? That would work, but I wouldn't have as high data integrity, correct? – erj1 Aug 05 '13 at 19:17
  • Found this question of SO: http://stackoverflow.com/questions/8334602/need-to-create-a-foreign-key-when-creating-a-table-on-rails. So per this answer, the model should define the data integrity relationships, and not the data store. – erj1 Aug 05 '13 at 20:09

0 Answers0