php 字符串作为 Laravel 迁移中的主键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44229280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String as Primary Key in Laravel migration
提问by DGeo
I've had to change a table in my database so that the primary key
isn't the standard increments
.
我不得不在我的数据库中更改一个表,以便它primary key
不是标准的increments
.
Here's the migration,
这是迁移,
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->text('code', 30)->primary();
$table->timestamps();
$table->text('name');
$table->text('comment');
});
}
However, MySQL keeps returning with,
但是,MySQL 不断返回,
Syntax error or access violation: 1170 BLOB/TEXT column 'code' used in key specification without a key length (SQL: alter table
settings
add primary keysettings_code_primary
(code
)
语法错误或访问冲突:1170 BLOB/TEXT 列“代码”用于没有键长度的键规范(SQL:alter table
settings
add primary keysettings_code_primary
(code
)
I've tried leaving the normal increments
id
in there and modifying the table in a different migration but the same thing happens.
我试过将正常increments
id
留在那里并在不同的迁移中修改表,但发生了同样的事情。
Any ideas of what I'm doing wrong?
关于我做错了什么的任何想法?
Laveral Version 5.4.23
Laveral Version 5.4.23
回答by Sandeesh
Change it to string.
将其更改为字符串。
$table->string('code', 30)->primary();