laravel 表定义不正确;只能有一个自动列,并且必须将其定义为键

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

Incorrect table definition; there can be only one auto column and it must be defined as a key

laravellaravel-4migration

提问by Sturm

I keep getting error while migrating in Laravel

我在 Laravel 中迁移时不断出错

[PDOException] 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

[PDOException] SQLSTATE[42000]:语法错误或访问冲突:1075 不正确的表定义;只能有一个自动列,并且必须将其定义为键

Code

代码

public function up()
    {
        Schema::create('inventories', function($table){

            $table->engine = 'InnoDB';

            $table->increments('id')->unsigned();
            $table->string('sku',255);
            $table->string('description', 255 )->nullable;
            $table->tinyInteger('stock',5)->nullable()->unsigned();
            $table->tinyInteger('day_of_week',1)->unsigned();
            $table->text('note')->nullable();

            $table->timestamps();

        });

    }

回答by Sturm

/**
 * Create a new tiny integer column on the table.
 *
 * @param  string  $column
 * @param  bool  $autoIncrement
 * @param  bool  $unsigned
 * @return \Illuminate\Support\Fluent
 */
public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
{
    return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
}

This is the tinyInteger()function from Blueprint.php. As you can see it expects a boolean parameter here. It looks like you're trying to add a argument for size. You cannot specify the size of tinyint in Laravel.

这是tinyInteger()来自Blueprint.php. 正如你所看到的,这里需要一个布尔参数。看起来您正在尝试为大小添加参数。您不能在 Laravel 中指定 tinyint 的大小。

    $table->engine = 'InnoDB';

    $table->increments('id');
    $table->string('sku',255);
    $table->string('description', 255 )->nullable();
    $table->tinyInteger('stock')->nullable()->unsigned();
    $table->tinyInteger('day_of_week')->unsigned();
    $table->text('note')->nullable();

This works fine.

这工作正常。

回答by zEELz

Just to add, $table->integer('user_id', 10)was also throwing that error so I removed the 'size' parameter as per Sturm's answer and after taking a look at the Blueprintclass now migrateworks.

只是补充一下,$table->integer('user_id', 10)也抛出了那个错误,所以我根据Sturm的回答删除了“大小”参数,并在查看了Blueprint类现在migrate工作后。

回答by Antiomic

Try this

尝试这个

$table->integer('user_id')->length(10)->unsigned();