Thu*_*sX3 3 dbal laravel laravel-5 laravel-5.4
我得到一个错误,当我尝试列的类型从字符串文本,随着Laravel的迁移功能。
文件:{data_time} _change_db_structure.php
public function up()
{
Schema::table('service_request_type', function (Blueprint $table) {
$table->dropIndex(['sro_key_group']);
$table->text('sro_key_group')->change();
$table->renameColumn('sro_key_group', 'tags');
});
}
Run Code Online (Sandbox Code Playgroud)
这是原单迁移创建表文件。
public function up()
{
Schema::create('service_request_type', function (Blueprint $table) {
$table->engine = 'InnoDB';
...
$table->string('sro_key_group', 100)->nullable()->index();
...
});
}
Run Code Online (Sandbox Code Playgroud)
我弄错了。
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'sro_key_group' used in key specification without a key length (SQL: ALTER TABLE service_request_type CHANGE sro_key_group sro _key_group TEXT DEFAULT NULL COLLATE utf8_unicode_ci)Run Code Online (Sandbox Code Playgroud)[Doctrine\DBAL\Driver\PDOException]SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'sro_key_group' used in key specification without a key length
Run Code Online (Sandbox Code Playgroud)[PDOException]SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'sro_key_group' used in key specification without a key length
What wrong? And I already install doctrine/dbal in my composer.json.
You'll need to do this in three steps, either using three separate migrations, or three calls to table() as you've shown in your answer.
The first issue is that, even though you've written your statements in the order you'd like them to execute (and the order they need to execute), the schema builder will actually rearrange the order so that "change" statements are executed first. The schema builder treats new columns and changed columns as "implied" statements, and moves them to the top of the stack of commands to run. However, rename statements are not considered "change" statements.
So, even though you've written the code to:
[
remove index,
change column from varchar to text,
rename column,
]
Run Code Online (Sandbox Code Playgroud)
The schema builder will actually execute:
[
change column from varchar to text,
remove index,
rename column,
]
Run Code Online (Sandbox Code Playgroud)
Now, since the change command is happening before the column is removed from the index, you are getting the 1170 error.
接下来的问题是试图做列更改,并在相同的上下文中列重命名。用于执行请求更改的SQL是通过执行架构差异生成的,但是两个架构差异都将在实际进行任何更改之前完成。所以,从第一个变化varchar,以text将生成相应的SQL进行改变,但随后的第二个变化来重命名列将实际生成的SQL更改列后面的文本字段,而重新命名它。
要解决这些问题,您可以创建三个迁移,其中第一个迁移简单地删除索引,第二个迁移,改变类型,然后第三迁移重命名它,或者你可以保持你的一个迁移和运行三个table()语句。
public function up()
{
// make sure the index is dropped first
Schema::table('service_request_type', function (Blueprint $table) {
$table->dropIndex(['sro_key_group']);
});
// now change the type of the field
Schema::table('service_request_type', function (Blueprint $table) {
$table->text('sro_key_group')->nullable()->change();
});
// now rename the field
Schema::table('service_request_type', function (Blueprint $table) {
$table->renameColumn('sro_key_group', 'tags');
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2202 次 |
| 最近记录: |