SQLSTATE [42000]:只能有一个自动列,必须将其定义为键

时间:2018-03-29 05:03:21

标签: php laravel

我在laravel中的代码下运行时遇到问题。我想通过迁移创建一个推荐表,但它认为up()中有多个主键

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTestimonialsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('testimonials', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',100);
            $table->text('comment');
            $table->integer('created_by',10);
            $table->integer('last_updated_by',10);
            $table->string('position',50);
            $table->string('address',100);
            $table->string('image');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admins');
    }
}

上面的代码显示错误说多个autoincrenet列..或bla bla ... 错误样本

$ php artisan migrate

   Illuminate\Database\QueryException  : 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 `testimonials` (`id` int unsigned not null auto_increment primary key, `name` varchar(100) not null, `comment` text not null, `created_by` int not null auto_increment primary
 key, `last_updated_by` int not null auto_increment primary key, `position` varchar(50) not null, `address` varchar(100) not null, `image` varchar(191) not null, `created_at` timestamp null, `
updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

  at C:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php: 664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   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")
      C:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 458

  2   PDOStatement::execute()
      C:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 458

  Please use the argument -v to see more details.

3 个答案:

答案 0 :(得分:1)

create table `testimonials` (
`id` int unsigned not null auto_increment primary key,
`name` varchar(100) not null, 
`comment` text not null, 
`created_by` int not null auto_increment primary key, 
`last_updated_by` int not null auto_increment primary key, 
`position` varchar(50) not null, 
`address` varchar(100) not null,
`image` varchar(191) not null, 
`created_at` timestamp null, 
`updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

在这里检查您生成的查询多个列是主键,这样就是错误所以您只需删除

$table->integer('created_by',10);
$table->integer('last_updated_by',10);

行和使用

$table->integer('created_by');
$table->integer('last_updated_by');

或者如果想要primary key,请转到composite primary key

public function up()
    {
        Schema::create('testimonials', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',100);
            $table->text('comment');
            $table->integer('created_by');
            $table->integer('last_updated_by');
            $table->string('position',50);
            $table->string('address',100);
            $table->string('image');
            $table->timestamps();
        });
        DB::statement('ALTER TABLE testimonials ADD CONSTRAINT check_created_by CHECK (created_by<=10)');
        DB::statement('ALTER TABLE testimonials ADD CONSTRAINT check_last_updated_by CHECK (last_updated_by<=10)');
    }

答案 1 :(得分:1)

方法整数的第二个参数是罪魁祸首,因为它以某种方式将该字段标记为主键。 在您的情况下,$table->increments('id');将使用“UNSIGNED INTEGER”等效项将该字段设置为增加ID(主键)。因此,删除主键设置方法并从整数方法中删除第二个参数。

希望这澄清了疑问:)

答案 2 :(得分:0)

试试这个:它会对你有所帮助。只需将此代码复制并粘贴到您的迁移文件中即可。

 public function up()
    {
        Schema::create('testimonials', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',100);
            $table->text('comment');
            $table->integer('created_by')->unsigned();
            $table->integer('last_updated_by')->unsigned();  
            $table->string('position',50);
            $table->string('address',100);
            $table->string('image');
            $table->timestamps();
        });
    }