![](/img/trans.png)
[英]PHP artisan migrate doesn't work for Laravel project, but show an error
[英]laravel 5.4 - php artisan migrate command doesn't work
我有四個遷移文件,當我在命令行中運行php artisan migrate
,它說:
Nothing to migrate
我也嘗試過php artisan migrate --database=vcp
:
Database [vcp] not configured.
我在.env
文件中使用了另一個數據庫並再次運行php artisan migrate
命令:
Migration table created successfully.
Nothing to migrate.
跑步
php artisan migrate:refresh
, php artisan migrate:reset
, php artisan migrate:status
, php artisan migrate --path="database/migrations/migration_file"
這些消息
Nothing to rollback.
Nothing to migrate.
No migrations found
和
composer dump-autoload or composer update
沒有幫助。
這是我的.env
文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=vcp
DB_USERNAME=root
DB_PASSWORD=secret
我的2017_05_10_201750_add_columns_to_users.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnsToUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('status')->default('online');
$table->string('api_token');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('status');
$table->dropColumn('api_token');
});
}
}
請幫忙!
正如您在上面的注釋中已經指出的那樣, The changes are done directly on database
。 然后Nothing to migrate.
是正確的。
嘗試了解概念,遷移在文件中包含表結構,並且在運行遷移時,這些表在數據庫中創建。 每當您在遷移文件中進行更改時,都必須再次運行遷移,以便將這些更改反映回數據庫表。 因此流程為:
migration file -> database table
不
database table -> migration file
這對我有用:
首先,刪除所有數據庫表,包括遷移表。 其次,您的AddColumnsToUsers文件的更新方法up()如下所示:
public function up()
{
DB::beginTransaction();
try {
Schema::table('users', function (Blueprint $table) {
$table->string('status')->default('online');
$table->string('api_token');
});
DB::commit();
} catch (PDOException $e) {
DB::rollBack();
$this->down();
}
}
這將檢查數據庫以查看表是否已經存在,如果不存在則創建表,如果存在則回滾。 讓我知道這是否有效。
有時您會像我一樣手動編寫遷移功能,復制並粘貼舊的,然后手動更改文件名。 實例:在文件2021_10_13_082805_ insert_fields_pages_table .PHP如果是同樣的情況,照顧到類名,它必須包含反映文件名的名稱。 在這個例子中:
class InsertFieldsPagesTable extends Migration
如果您忘記更改類名,遷移將什么也不做,什么也不做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.