Laravel 表:只能有一个自动列,并且必须定义为键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28884918/
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
Laravel table: there can be only one auto column and it must be defined as a key
提问by Smiley
I have made all integers unsigned but I still get the error. What do I need to change?
我已将所有整数设为无符号,但仍然出现错误。我需要改变什么?
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFacebook extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('facebook', function($table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
$table->string('username', 255);
$table->bigInteger('uid', 20)->unsigned();
$table->string('access_token', 255);
$table->string('access_token_secret', 255);
$table->string('photoURL', 255);
$table->string('profileURL', 255);
$table->string('firstName', 255);
$table->string('lastName', 255);
$table->string('gender', 255);
$table->string('age', 20);
$table->integer('birthDay')->unsigned();
$table->integer('birthMonth')->unsigned();
$table->integer('birthYear')->unsigned();
$table->string('email', 255);
$table->string('phone', 30);
$table->string('address', 255);
$table->string('country', 100);
$table->string('region', 100);
$table->string('city', 100);
$table->string('zip', 20);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('facebook');
}
}
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be de fined as a key
SQLSTATE[42000]:语法错误或访问冲突:1075 表定义不正确;自动列只能有一个,必须定义为键
Thank you.
谢谢你。
回答by newatol
Use
用
$table->bigInteger('uid')->unsigned();
instead of
代替
$table->bigInteger('uid', 20)->unsigned();
Infos:
信息:
in /vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint
you can see the bigInteger function:
在/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint
你可以看到 bigInteger 函数:
public function bigInteger($column, $autoIncrement = false, $unsigned = false)
So 20 (not being 0) evals to true
.
所以 20(不是 0)评估为true
.
While string
method has as second parameter length
:
虽然string
方法具有第二个参数length
:
public function string($column, $length = null)
Therefore if you use any integer blueprint (bigInteger
, mediumInteger
, tinyInteger
, smallInteger
, etc...) with any second parameter (other than 0) you are telling Laravel to make an integer with an auto_increment
property, this will return:
因此,如果您使用任何带有任何第二个参数(0 除外)的整数蓝图(bigInteger
, mediumInteger
, tinyInteger
, smallInteger
, 等...),您是在告诉 Laravel 使用auto_increment
属性创建一个整数,这将返回:
Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")
Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")
回答by habib
Specify the size for bigInteger column by following way
通过以下方式指定 bigInteger 列的大小
$table->bigInteger('uid')->length(20)->unsigned();