Advertisement
Guest User

Untitled

a guest
Jul 9th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. Integrity constraint violation: 1452 Cannot add
  2. or update a child row: a foreign key constraint fails (`twitter`.
  3. `tweets`, CONSTRAINT `tweets_user_id_foreign` FOREIGN KEY (`user_
  4. id`) REFERENCES `users` (`id`) ON DELETE CASCADE)
  5.  
  6. <?php
  7.  
  8. use IlluminateSupportFacadesSchema;
  9. use IlluminateDatabaseSchemaBlueprint;
  10. use IlluminateDatabaseMigrationsMigration;
  11.  
  12. class CreateTweetsTable extends Migration
  13. {
  14. /**
  15. * Run the migrations.
  16. *
  17. * @return void
  18. */
  19. public function up()
  20. {
  21. Schema::create('tweets', function (Blueprint $table) {
  22. $table->increments('id');
  23. $table->integer('user_id')->unsigned()->index();
  24. $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
  25. $table->string('body', 140);
  26. $table->timestamps();
  27. });
  28. }
  29.  
  30. /**
  31. * Reverse the migrations.
  32. *
  33. * @return void
  34. */
  35. public function down()
  36. {
  37. Schema::dropIfExists('tweets');
  38. }
  39. }
  40.  
  41. <?php
  42.  
  43. namespace App;
  44.  
  45. use IlluminateDatabaseEloquentModel;
  46.  
  47. class Tweet extends Model
  48. {
  49. protected $fillable = [
  50. 'user_id', 'body',
  51. ];
  52. }
  53.  
  54. <?php
  55.  
  56. use AppTweet;
  57. use IlluminateDatabaseSeeder;
  58.  
  59. class TweetsTableSeeder extends Seeder
  60. {
  61. /**
  62. * Run the database seeds.
  63. *
  64. * @return void
  65. */
  66. public function run()
  67. {
  68. factory(Tweet::class, 10)->create([
  69. 'user_id' => 2
  70. ]);
  71. }
  72. }
  73.  
  74. <?php
  75.  
  76. /*
  77. |--------------------------------------------------------------------------
  78. | Model Factories
  79. |--------------------------------------------------------------------------
  80. |
  81. | Here you may define all of your model factories. Model factories give
  82. | you a convenient way to create models for testing and seeding your
  83. | database. Just tell the factory how a default model should look.
  84. |
  85. */
  86.  
  87. /** @var IlluminateDatabaseEloquentFactory $factory */
  88. $factory->define(AppUser::class, function (FakerGenerator $faker) {
  89. static $password;
  90.  
  91. return [
  92. 'name' => $faker->name,
  93. 'email' => $faker->unique()->safeEmail,
  94. 'password' => $password ?: $password = bcrypt('secret'),
  95. 'remember_token' => str_random(10),
  96. ];
  97. });
  98.  
  99. $factory->define(AppTweet::class, function (FakerGenerator $faker) {
  100. return [
  101. 'body' => $faker->realText(140),
  102. ];
  103. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement