Yan*_*lan 3 php mysql laravel laravel-5.3 laravel-5.4
我使用的是Laravel 5.3,迁移对于控制数据库开发来说非常棒.
我的问题是当我将列类型从字符串更改为文本时,一切都运行良好.但是在用户保存长度超过255(varchar)的数据之后.然后我的迁移无法回滚.它会说数据太长了我的专栏.想问大家如何解决这个问题?
================================================== =======
Schema::table('tbname', function(Blueprint $table)
{
$table->text('value')->change();
});
Schema::table('tbname', function(Blueprint $table)
{
$table->string('value')->change();
});
Run Code Online (Sandbox Code Playgroud)
================================================== =======
播种机:
$records = [
[
'description' => 'The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. '
],
[
'description' => 'The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. '
]
];
foreach ($records as $index => $record) {
Desc::create($record);
}
Run Code Online (Sandbox Code Playgroud)
pat*_*cus 10
您的text字段中包含超过255个字符.在回滚中,您尝试将字段更改为varchar(255).此时,MySQL决定:要么截断数据以适应255个字符,要么抛出错误.
如果在严格模式下使用MySQL,MySQL将抛出错误.如果不使用严格模式,MySQL将截断数据而不会出错.
Laravel 5.3默认更改为使用严格模式.
如果要停止收到此错误,可以通过'strict' => false在config/database.php文件中设置来禁用严格模式.
请注意,虽然这将抑制错误,但会产生后果.之前导致此错误的数据现在只会被截断而不会出现任何错误.
如果您只想为回滚禁用严格模式,则可以创建第二个禁用严格模式的数据库连接,并告知迁移使用该连接进行回滚.但请记住,这将截断此字段中超过255个字符的所有数据.
配置:
'connections' => [
'mysql' => [
/* normal connection information */
'strict' => true,
],
'mysql-rollback' => [
/* normal connection information */
'strict' => false,
],
],
Run Code Online (Sandbox Code Playgroud)
移民:
public function down()
{
Schema::connection('mysql-rollback')->table('tbname', function(Blueprint $table) {
$table->string('value')->change();
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
825 次 |
| 最近记录: |