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

4 migration laravel laravel-4

我在Laravel迁移时一直遇到错误

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

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();

        });

    }
Run Code Online (Sandbox Code Playgroud)

Stu*_*urm 16

/**
 * 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'));
}
Run Code Online (Sandbox Code Playgroud)

这是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();
Run Code Online (Sandbox Code Playgroud)

这很好用.

  • 是的,所以基本上发生了什么是你在tinyInteger函数中传递5,它看到为真并尝试自动递增该列,这就是你得到错误的原因 (6认同)