简体   繁体   English

如何在Laravel 5.3迁移中将十进制列添加到现有表

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

I have tried following 3 codes in a Laravel 5.3 project to add new decimal column to an existing table. 我尝试在Laravel 5.3项目中遵循3个代码,将新的十进制列添加到现有表中。 But it's giving same error every time. 但它每次都会给出同样的错误。

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

and

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

and

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

The error was : 错误是:

[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) 

am i missing something ? 我错过了什么吗?

You're trying to use ->addColumn() which isn't quite the right syntax. 您正在尝试使用->addColumn() ,这不是正确的语法。

Create a new migration, php artisan make:migrate add_cost_column_to_mileages . 创建一个新的迁移, php artisan make:migrate add_cost_column_to_mileages

Then inside your migration, you want it to look like this for up: 然后在迁移中,您希望它看起来像这样:

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

And this for down: 这是为了下来:

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

This is from the docs here , although they don't make it explicitly clear. 这是来自这里的文档,虽然它们没有明确说明。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM