Laravel 4 - 工匠错误 SQLSTATE[42000]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14931802/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:34:23  来源:igfitidea点击:

Laravel 4 - Artisan error SQLSTATE[42000]

phplaravel

提问by ChrisBratherton

I am trying to create a new migration for my users table, I have the following schema:

我正在尝试为我的用户表创建一个新的迁移,我有以下架构:

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

When I try to run php artisan migrate from the terminal, I get the following error:

当我尝试从终端运行 php artisan migrate 时,出现以下错误:

[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(idint unsigne d not null auto_increment primary key, usernamevarchar(16) not null, passwordvarchar(64) no t null, roleint not null auto_increment primary key, created_attimestamp default 0 not null , updated_attimestamp default 0 not null)) (Bindings: array (
))

[异常]
SQLSTATE[42000]:语法错误或访问冲突:1075 表定义不正确;只能有一个自动列并且它必须被定义为一个键(SQL:创建表usersidint unsigne d not null auto_increment primary key, usernamevarchar(16) not null, passwordvarchar(64) no t null, roleint not null auto_increment primary键,created_at时间戳默认 0 不为空,updated_at时间戳默认 0 不为空))(绑定:数组(
))

The error has something to do with the 'role' field, as when this is removed it seems to run fine.

该错误与“角色”字段有关,因为当它被删除时,它似乎运行良好。

Thanks in advance for any help or insight.

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

回答by Robbo

The second parameter for integeris an auto increment flag.

for的第二个参数integer是自动递增标志。

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

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

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

回答by Jonh Mark

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

That fixes it.

那修复它。

回答by Nidhin

Length attribute of integer is not permitted.Remove it and try.

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