我跑去php artisan migrate:fresh洗掉每個表,然后我有一個 sql 轉儲檔案,它還創建表并用一些資料填充它們。但是,我還需要運行,php artisan migrate因為需要進行一些遷移才能向某些表添加一些額外的列(并且這些列不在我正在使用的 sql 轉儲檔案中)。
所以,我跑php artisan migrate我總是得到一些像這樣的錯誤:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists (SQL: create table `categories` (`id` bigint unsigned not null auto_increment primary key, `title` varchar(255) not null, `description` varchar(255) not null, `is_active` tinyint(1) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
事實上,該表已經存在,但是除了這個之外,還有一些其他遷移需要運行以創建類別表。有一些方法可以忽略這個錯誤或類似的東西,所以其他還沒有完成作業的遷移可以執行嗎?
uj5u.com熱心網友回復:
檢查表是否已經存在:
if (Schema::hasTable('categories')) {
// The "categories" table exists...
}
如果要更改表,可以添加列或編輯現有列。查看列修飾符https://laravel.com/docs/9.x/migrations#column-modifiers
uj5u.com熱心網友回復:
有兩種方法可以做到這一點。1. 注釋create table migration里面的代碼。它將跳過該幫助運行所有其他幫助。2. DB中有一張名為migrations的表,確保migration已經在表中。
uj5u.com熱心網友回復:
如果您想洗掉表中的所有內容以執行所謂的 a fresh migration,您可以在終端中運行以下代碼:
php artisan migrate:fresh
再次警告,運行此命令將洗掉表中的每個表、列、行和單元格,因此在開始alter遷移程序之前在開發中執行是合乎邏輯的。
create_categories_table 沒有出現在遷移表上,但你知道為什么它顯示已經存在的錯誤嗎?
嘗試洗掉快取的所有內容以確保沒有快取任何內容,但我不完全確定遷移程序是否可以通過這種方式修復。嘗試以下行真的沒有什么壞處:
php artisan optimize:clear
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/432537.html
