【问题标题】:SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition;SQLSTATE[42000]:语法错误或访问冲突:1075 表定义不正确;
【发布时间】:2019-09-16 18:59:54
【问题描述】:

迁移文件

$table->increments('id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('band_id')->references('id')->on('bands');
$table->foreign('genre_id')->references('id')->on('genres');
$table->foreign('cate_id')->references('id')->on('cates');
$table->foreign('type_id')->references('id')->on('types');
$table->integer('status');
$table->date('date');
$table->time('time');
$table->decimal('price');
$table->tinyIncrements('instrument');
$table->string('instrument_detail',255);
$table->timestamps();

运行 php artisan migrate 后

SQLSTATE[42000]:语法错误或访问冲突:1075 不正确 表定义;只能有一个自动列,并且必须是 定义为键 (SQL: create table bookings (id int unsigned not null auto_increment 主键,status int not null,date 日期不为空,time 时间不为空,price 十进制(8, 2) 不为空, instrument tinyint unsigned not null auto_increment 主键, instrument_detail varchar(255) 不为空,created_at 时间戳 null, updated_at timestamp null) 默认字符集 utf8mb4 整理 utf8mb4_unicode_ci)

下面是这个

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

【问题讨论】:

    标签: php laravel artisan-migrate


    【解决方案1】:
    $table->unsignedTinyInteger('instrument', true);
    

    第二个参数是bool,自动递增默认为false

    【讨论】:

      【解决方案2】:

      下一句:

      $table->foreign('user_id')->references('id')->on('users');
      

      只是告诉数据库在父/外列之间建立链接,但为了做到这一点,该列必须先前存在,所以:

      $table->unsignedInteger('user_id'); // first this
      $table->foreign('user_id')->references('id')->on('users'); // then this
      

      您应该对每个外键都这样做。

      注意:

      Laravel 不需要你定义这个链接,因为它没有使用这个,只是为了数据库的一致性。

      【讨论】:

      • 感谢 HCK :( 但仍然无法迁移他们说 SQLSTATE[42000]: 语法错误或访问冲突:1075 以同样的方式
      • @chojon 检查 Ahmed 的解决方案。
      • 所以我尝试了一下,但没有成功,我想知道为什么他们仍然无法运行 migrate 然后我会找到一些相同的情况这个问题谢谢 pow
      猜你喜欢
      • 2015-10-12
      • 2023-04-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      相关资源
      最近更新 更多