繁体   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