2013-02-18 119 views
5

我想创建为我的用户表的新移民,我有以下模式:Laravel 4 - 工匠错误SQLSTATE [42000]

 Schema::create('users', function($t) { 
      $t->increments('id'); 
      $t->string('username', 16); 
      $t->string('password', 64); 
      $t->integer('role', 64); 
      $t->timestamps(); 
    }); 

当我尝试运行PHP的工匠从终端迁移,我得到以下错误:

[Exception]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: create table users (id int unsigne d not null auto_increment primary key, username varchar(16) not null, password varchar(64) no t null, role int not null auto_increment primary key, created_at timestamp default 0 not null , updated_at timestamp default 0 not null)) (Bindings: array (
))

错误有事情做与“角色”字段,因为当这是删除它看上去一切正常。

在此先感谢您的帮助或见解。

+0

为什么生成的SQL将'role'设置为'auto_increment primary key'? 'id'是唯一的'auto_increment主键'列有什么问题? – 2013-02-18 08:07:36

回答

13

integer的第二个参数是一个自动增量标志。

public function integer($column, $autoIncrement = false, $unsigned = false) 

https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Schema/Blueprint.php#L510

+0

太棒了,感谢您的帮助和资源:)诀窍! – ChrisBratherton 2013-02-18 08:18:45

+0

@TheRabbitFactory,我知道它是一段时间以前,但你知道我需要做什么,因为我有和你一样的问题 – Thom 2014-06-01 14:12:19

+0

我不记得,但我想我基本上缺少第二个参数。 – ChrisBratherton 2014-06-01 14:25:21

0
$t->integer('role', false); 

修复它。

+3

你能解释它为什么修复它吗? – 2015-07-01 17:56:56

+0

没有解释的答案是没有用的。 – 2015-07-02 10:13:23

+0

通过设置整数为false我禁用autoIncrement这就是为什么我们绕过错误。 – 2015-07-30 05:47:01

0

整数的长度属性是不允许的。删除它并尝试。

相关问题