我尝试在 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');
});
这是来自文档 here ,尽管他们没有明确说明。
关于php - 如何在 Laravel 5.3 迁移中将小数列添加到现有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42222583/