MacOSのローカルにてサンプルアプリを作成しています。
UdemyやQiitaなどを参考にしながら作成していましたが、
現在画像アップロード機能を追加するために
- migrationファイル(
2021_05_23_142304_add_main_image_to_recipes_table.php
)を作成、実行 php artisan migrate
を実行
の手順を踏みましたが、SequelProでテーブルを確認するとカラムが追加されていません。
現状は何が原因なのか分からず困っています。
わかる方がいらっしゃいましたら教えていただきますようよろしくお願いいたします。
問題解決に必要なファイルがありましたら随時追加致しますのでよろしくお願いいたします。
動作環境
PHP: 7.3.11
Laravel: 5.8
MySQL: 5.7
エラー内容
-
php artisan migrate
を実行してもMigrated: 2021_05_23_142304_add_main_image_to_recipes_table
が表示されない -
php artisan migrate:reset
を実行してみると下記のエラーログが表示されます。
bash
1$ php artisan migrate:reset 2Rolling back: 2021_05_23_142304_add_main_image_to_recipes_table 3 4 Illuminate\Database\QueryException : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.recipes' doesn't exist (SQL: alter table `recipes` drop `main_image`) 5 6 at /Users/yr/progs/laravel/easyCookingMediaApp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664 7 660| // If an exception occurs when attempting to run a query, we'll format the error 8 661| // message to include the bindings with SQL, which will make this exception a 9 662| // lot more helpful to the developer instead of just the database's errors. 10 663| catch (Exception $e) { 11 > 664| throw new QueryException( 12 665| $query, $this->prepareBindings($bindings), $e 13 666| ); 14 667| } 15 668| 16 17 Exception trace: 18 19 1 PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.recipes' doesn't exist") 20 /Users/yr/progs/laravel/easyCookingMediaApp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458 21 22 2 PDOStatement::execute() 23 /Users/yr/progs/laravel/easyCookingMediaApp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458 24 25 Please use the argument -v to see more details.
期待する動作
- migrationが問題なく動作し、
main_image
カラムがテーブルに追加されること php artisan migration:reset
などのコマンドを実行しても問題なく正常に動作すること
試したこと
1.composer dump-autoload
(下記のログが表示される)
bash
1$ composer dump-autoload 2 3Generating optimized autoload files 4Carbon 1 is deprecated, see how to migrate to Carbon 2. 5https://carbon.nesbot.com/docs/#api-carbon-2 6 You can run './vendor/bin/upgrade-carbon' to get help in updating carbon and other frameworks and libraries that depend on it. 7> Illuminate\Foundation\ComposerScripts::postAutoloadDump 8> @php artisan package:discover 9Discovered Package: beyondcode/laravel-dump-server 10Discovered Package: fideloper/proxy 11Discovered Package: laravel/nexmo-notification-channel 12Discovered Package: laravel/slack-notification-channel 13Discovered Package: laravel/tinker 14Discovered Package: nesbot/carbon 15Discovered Package: nunomaduro/collision 16Package manifest generated successfully. 17Generated optimized autoload files containing 4111 classes
2.php artisan migrate
(下記のログが表示される)
bash
1$ php artisan migrate 2Migrating: 2014_10_12_000000_create_users_table 3Migrated: 2014_10_12_000000_create_users_table 4Migrating: 2014_10_12_100000_create_password_resets_table 5Migrated: 2014_10_12_100000_create_password_resets_table 6Migrating: 2021_05_18_015254_create_recipes_table 7Migrated: 2021_05_18_015254_create_recipes_table 8Migrating: 2021_05_19_213625_add_deleted_at_to_recipes_table 9Migrated: 2021_05_19_213625_add_deleted_at_to_recipes_table 10Migrating: 2021_05_20_043807_add_description_to_recipes_table 11Migrated: 2021_05_20_043807_add_description_to_recipes_table 12Migrating: 2021_05_20_052550_add_comment_accepted_to_recipes_table 13Migrated: 2021_05_20_052550_add_comment_accepted_to_recipes_table 14Migrating: 2021_05_20_150532_add_cookingtools_to_recipes_table 15Migrated: 2021_05_20_150532_add_cookingtools_to_recipes_table
Migrated: 2021_05_23_142304_add_main_image_to_recipes_table
は実行されない。
該当しそうなコード
php
1// 2021_05_23_142304_add_main_image_to_recipes_table.php 2<?php 3 4use Illuminate\Support\Facades\Schema; 5use Illuminate\Database\Schema\Blueprint; 6use Illuminate\Database\Migrations\Migration; 7 8class AddMainImageToRecipesTable extends Migration 9{ 10 /** 11 * Run the migrations. 12 * 13 * @return void 14 */ 15 public function up() 16 { 17 Schema::table('recipes', function (Blueprint $table) { 18 $table->string('main_image')->default('no_image.jpg'); 19 }); 20 } 21 22 /** 23 * Reverse the migrations. 24 * 25 * @return void 26 */ 27 public function down() 28 { 29 Schema::table('recipes', function (Blueprint $table) { 30 $table->dropColumn('main_image'); 31 }); 32 } 33} 34
migrationsテーブルの状態
bash
130 2021_05_23_142304_add_main_image_to_recipes_table 4 291 2014_10_12_000000_create_users_table 5 392 2014_10_12_100000_create_password_resets_table 5 493 2021_05_18_015254_create_recipes_table 5 594 2021_05_19_213625_add_deleted_at_to_recipes_table 5 695 2021_05_20_043807_add_description_to_recipes_table 5 796 2021_05_20_052550_add_comment_accepted_to_recipes_table 5 897 2021_05_20_150532_add_cookingtools_to_recipes_table 5
php
1// 2021_05_18_015254_create_recipes_table.php 2<?php 3 4use Illuminate\Support\Facades\Schema; 5use Illuminate\Database\Schema\Blueprint; 6use Illuminate\Database\Migrations\Migration; 7 8class CreateRecipesTable extends Migration 9{ 10 /** 11 * Run the migrations. 12 * 13 * @return void 14 */ 15 public function up() 16 { 17 Schema::create('recipes', function (Blueprint $table) { 18 $table->increments('id'); 19 $table->string('name', 50); 20 $table->integer('cookingtime'); 21 $table->timestamps(); 22 }); 23 } 24 25 /** 26 * Reverse the migrations. 27 * 28 * @return void 29 */ 30 public function down() 31 { 32 Schema::dropIfExists('recipes'); 33 } 34} 35
php
1// config/database.php 2<?php 3 4return [ 5 6 7 'default' => env('DB_CONNECTION', 'mysql'), 8 9 10 'connections' => [ 11 12 'sqlite' => [ 13 'driver' => 'sqlite', 14 'database' => env('DB_DATABASE', database_path('database.sqlite')), 15 'prefix' => '', 16 ], 17 18 'mysql' => [ 19 'driver' => 'mysql', 20 'host' => env('DB_HOST', '127.0.0.1'), 21 'port' => env('DB_PORT', '3306'), 22 'database' => env('DB_DATABASE', 'homestead'), 23 'username' => env('DB_USERNAME', 'homestead'), 24 'password' => env('DB_PASSWORD', 'secret'), 25 'unix_socket' => env('DB_SOCKET', ''), 26 'charset' => 'utf8mb4', 27 'collation' => 'utf8mb4_unicode_ci', 28 'prefix' => '', 29 'strict' => true, 30 'engine' => null, 31 ], 32 33 'pgsql' => [ 34 'driver' => 'pgsql', 35 'host' => env('DB_HOST', '127.0.0.1'), 36 'port' => env('DB_PORT', '5432'), 37 'database' => env('DB_DATABASE', 'forge'), 38 'username' => env('DB_USERNAME', 'forge'), 39 'password' => env('DB_PASSWORD', ''), 40 'charset' => 'utf8', 41 'prefix' => '', 42 'schema' => 'public', 43 'sslmode' => 'prefer', 44 ], 45 46 'sqlsrv' => [ 47 'driver' => 'sqlsrv', 48 'host' => env('DB_HOST', 'localhost'), 49 'port' => env('DB_PORT', '1433'), 50 'database' => env('DB_DATABASE', 'forge'), 51 'username' => env('DB_USERNAME', 'forge'), 52 'password' => env('DB_PASSWORD', ''), 53 'charset' => 'utf8', 54 'prefix' => '', 55 ], 56 57 ], 58 59 'migrations' => 'migrations', 60 61 62 'redis' => [ 63 64 'client' => 'predis', 65 66 'default' => [ 67 'host' => env('REDIS_HOST', '127.0.0.1'), 68 'password' => env('REDIS_PASSWORD', null), 69 'port' => env('REDIS_PORT', 6379), 70 'database' => env('REDIS_DB', 0), 71 ], 72 73 'cache' => [ 74 'host' => env('REDIS_HOST', '127.0.0.1'), 75 'password' => env('REDIS_PASSWORD', null), 76 'port' => env('REDIS_PORT', 6379), 77 'database' => env('REDIS_CACHE_DB', 1), 78 ], 79 80 ], 81 82]; 83
あなたの回答
tips
プレビュー