BugFix:手动删除迁移文件&无法添加外键约束

  • 当你手动删除迁移文件后会报错:
[ErrorException]
  include(D:\phptools\Apache2.4\Apache24\htdocs\library_management_system\vendor\compos
  er/../../database/migrations/2017_09_24_012657_create_password_resets_table.php.php):
   failed to open stream: No such file or directory

此时需要执行composer dump-autoload 复位composer自动加载文件

  • 生成数据表时报错
 [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter ta
  ble `borrows` add constraint `borrows_book_item_id_foreign` foreign key (`book_item_i
  d`) references `book_items` (`id`))

首先要检查你定义的外键的字段类型是否为unsignedInteger.
其次检查你的migrations里面创建数据表的顺序,由于migration是按照你创建的数据表的顺序执行的,所以如果在关联的数据表还没有创建之前就生成需要外键的数据表就会报错。

最后还是报错:


  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL
   syntax; check the manual that corresponds to your MySQL server version for the right
   syntax to use near ')' at line 1 (SQL: alter table `book_items` add constraint `book
  _items_book_id_foreign` foreign key (`book_id`) references `books` ())

最后的原因是:-(
我把$table->foreign('user_id')->references('id')->on('users');写成了$table->foreign('user_id')->reference('id')->on('users'); 实在太粗心了。

你可能感兴趣的:(BugFix:手动删除迁移文件&无法添加外键约束)