首页 > 解决方案 > Laravel 数据库迁移 表定义不正确

问题描述

public function up()
    {
        Schema::create('materials', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',60);
            $table->integer('category_id',10);
            $table->integer('low_bulk',10);
            $table->integer('high_bulk',10);
            $table->integer('order_level',10);
            $table->decimal('unit_price',10,2);
            $table->string('description');
            $table->timestamps();
        });
    }

此架构给了我以下错误

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

谁能告诉我应该改变什么?

标签: laravelsyntaxmigration

解决方案


的第二个参数integer()不是长度,而是列是否应该自动递增。PHP 转换10true并尝试使所有这些列自动递增。

去掉它,长度对 MySQL 没有任何影响:

Schema::create('materials', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name',60);
    $table->integer('category_id');
    $table->integer('low_bulk');
    $table->integer('high_bulk');
    $table->integer('order_level');
    $table->decimal('unit_price',10,2);
    $table->string('description');
    $table->timestamps();
});

推荐阅读