簡體   English   中英

如何在Laravel 5.3遷移中將十進制列添加到現有表

[英]How to add decimal column to existing table in Laravel 5.3 migration

我嘗試在Laravel 5.3項目中遵循3個代碼,將新的十進制列添加到現有表中。 但它每次都會給出同樣的錯誤。

Schema::table('mileages', function (Blueprint $table) {
    $table->addColumn('decimal', 'cost');
});

Schema::table('mileages', function (Blueprint $table) {
    $table->addColumn('decimal', 'cost', ['default'=>0]);
});

Schema::table('mileages', function (Blueprint $table) {
    $table->addColumn('decimal', 'cost', ['default'=>'0,0']);
});

錯誤是:

[Illuminate\Database\QueryException]                                                                                 
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that  
corresponds to your MariaDB server version for the right syntax to use near ' ) not null' at line 1 (SQL: alter table `mileages` add `cost` decimal (, ) not null) 

我錯過了什么嗎?

您正在嘗試使用->addColumn() ,這不是正確的語法。

創建一個新的遷移, php artisan make:migrate add_cost_column_to_mileages

然后在遷移中,您希望它看起來像這樣:

Schema::table('mileages', function (Blueprint $table) {
    $table->decimal('cost', 5,2); //Substitute 5,2 for your desired precision
});

這是為了下來:

Schema::table('mileages', function (Blueprint $table) {
    $table->dropColumn('cost');
});

這是來自這里的文檔,雖然它們沒有明確說明。

 Schema::table('mileages', function (Blueprint $table) { $table->addColumn('decimal',2); }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM