【问题标题】:Incorrect table definition; there can be only one auto column and it must be defined as a key表定义不正确;只能有一个自动列,并且必须将其定义为键
【发布时间】:2015-04-05 22:55:56
【问题描述】:

我在 Laravel 中迁移时不断出错

[PDO异常] 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();

        });

    }

【问题讨论】:

  • 您不必致电unsigned()increments() 已经这样做了。但是我怀疑这会导致错误...
  • 感谢您的提示。 :D

标签: laravel laravel-4 migration


【解决方案1】:
/**
 * 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'));
}

这是来自Blueprint.phptinyInteger() 函数。如您所见,这里需要一个布尔参数。看起来您正在尝试添加大小参数。你不能在 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();

这很好用。

【讨论】:

  • 是的,所以基本上发生的情况是您在 tinyInteger 函数中传递 5,该函数将其视为 true 并尝试自动增加该列,这就是您收到错误的原因
【解决方案2】:

试试这个

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

【讨论】:

    【解决方案3】:

    补充一点,$table->integer('user_id', 10) 也抛出了这个错误,所以我根据Sturm 的回答删除了“size”参数,在查看了Blueprint 类之后,现在migrate 可以工作了。

    【讨论】:

    • 但是我需要添加size参数,我该怎么办?
    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2020-01-01
    • 2013-12-17
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多