我在 Laravel 5.2 中没有问题,但在为用户模型创建迁移后在 Laravel 5.3 中,它显示了以下错误:
SQLSTATE[HY000]: General error: 1364 Field 'family' doesn't have a default value !!!
在模型用户中:
protected $fillable = [
'name', 'email', 'password', 'family', 'mobile', 'address', 'status'
];
Run Code Online (Sandbox Code Playgroud)
在迁移中:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('family');
$table->string('mobile')->unique();
$table->string('address');
$table->boolean('status');
$table->string('email')->unique();
$table->string('password');
$table->integer('reagent');
$table->rememberToken();
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
我的问题在哪里?
您应该添加->nullable()或->default('somethingHere')到您发送空值的字段。
$table->string('family')->nullable(); //this means that if you send empty value this field will become MySQL NULL
Run Code Online (Sandbox Code Playgroud)
或者设置默认值:
$table->string('family')->default('default value here');
Run Code Online (Sandbox Code Playgroud)
比移民:
php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)
和
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
小智 5
我在这里找到了我的解决方案链接: Eloquent create say column has no default value
答案在底部,我也只是引用了答案
imbhavin95 回复 8 个月前
然而,现在发生这种情况的原因是 Laravel 5.1 默认情况下对 MySQL 使用严格模式。
如果您想恢复到以前的行为,请更新您的 config/database.php 文件并为您的连接设置 'strict' => false 。
归功于此人https://laravel.io/user/imbhavin95
你可以做到nullable:
$table->string('family')->nullable();
Run Code Online (Sandbox Code Playgroud)
或者添加一些默认值:
$table->string('family')->default('none');
Run Code Online (Sandbox Code Playgroud)
之后您应该备份数据并运行:
php artisan migrate:refresh
Run Code Online (Sandbox Code Playgroud)
然后恢复数据。
或者您可以创建一个单独的迁移并只需更改family为 nullable:
Schema::table('users', function (Blueprint $table) {
$table->string('family')->nullable()->change();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18043 次 |
| 最近记录: |