初学laravel migrate常见错误解决

开发 后端
断断续续开始 laravel 入门学习,想整个简单的通讯录系统,设立了两个表,一个 branches ,一个 contacts。在创建 migration 文件的时候,没有考虑仔细,先把 contacts 表建立了,contacts 表有个外键连接到 branches 的 id,结果执行 migrate 命令的时候,出现以下错误。

断断续续开始 laravel 入门学习,想整个简单的通讯录系统,设立了两个表,一个 branches ,一个 contacts。在创建 migration 文件的时候,没有考虑仔细,先把 contacts 表建立了,contacts 表有个外键连接到 branches 的 id,结果执行 migrate 命令的时候,出现以下错误:

  1. [Illuminate\Database\QueryException] 
  2.  
  3. SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `contacts` add constraint `contac 
  4.  
  5. ts_branch_id_foreign` foreign key (`branch_id`) references `branches` (`id`) on delete cascade
  6.  
  7. [PDOException] 
  8.  
  9. SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint  

初步怀疑是表创建先后不规范造成,于是,手动修改 branches 的 migration 文件名称上的日期,再执行

  1. php artisan migrate:reset 

出现如下错误:

  1. [ErrorException] 
  2.  
  3. include(/Users/Ade/www/laravel_phonebook5.2): failed to open stream: Operation now in progress  

failed to open stream 错误解决

光看错误提示不是很理解,我们查看 laravel 的 log 文件

  1. more storage/logs/laravel.log 

找到出现 ERROR 的那段话:

  1. [2016-09-29 18:05:35] local.ERROR: exception 'ErrorException' with message 'include(/Users/Ade/www/laravel_phonebook5.2): failed to open stream: Operation now in progress' in /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php:412 
  2. Stack trace: 
  3. #0 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(412): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'include(/Users/...''/Users/Ade/www/...', 412, Array) 
  4. #1 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(412): Composer\Autoload\includeFile() 
  5. #2 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(301): Composer\Autoload\includeFile('/Users/Ade/www/...'
  6. #3 [internal function]: Composer\Autoload\ClassLoader->loadClass('CreateBranchesT...'
  7. #4 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(335): spl_autoload_call('CreateBranchesT...'
  8. #5 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(227): Illuminate\Database\Migrations\Migrator->resolve('2016_09_12_1728...'
  9. #6 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(206): Illuminate\Database\Migrations\Migrator->runDown(Object(stdClass), false

错误出现在 ClassLoader.php 文件的 412 行

查看改行代码,发现是一个调用文件的语句: 

而这个文件,在 log 文件中已经指出,即 resolve('2016_09_12_1728...') 。log 提示的这个名称,就是我修改的 branch 的 migration 文件名称。

我们再搜搜正常的 migration 文件会在哪些地方出现:

  1. mdfind 2014_10_12_000000_create_users_table.php|grep phonebook 

可见,正常的有 3 个地方出现,修改过的只有 1 个地方出现。

编辑这两个未出现的文件

调整 autoload_static.php 文件

发现 vendor/composer/autoload_static.php 文件中,和 branches 相关的语句如下:

  1. 'CreateBranchesTable' => __DIR__ ., 

想来应该是改名的时候,PHP Storm自动帮我把这个文件里面有关 branches 文件路径全部给删掉了。加回去就好了。

参照正常的 migration 文件名的配置情况,补充为

  1. 'CreateBranchesTable' => __DIR__ . '/../..' . '/database/migrations/2016_09_12_172822_create_branches_table.php'

调整 autoload_classmap.php 文件

我们发现 autoload_classmap.php 文件中,有关 branches 的路径名还是修改前的路径:

  1. 'CreateBranchesTable' => $baseDir . '/database/migrations/2016_09_29_172822_create_branches_table.php'

将其修改为

  1. 'CreateBranchesTable' => $baseDir . '/database/migrations/2016_09_12_172822_create_branches_table.php'

再执行 migrate 命令

  1. php artisan migrate:reset 

 OK,刚才的错误没了,不过我们又发现 contacts 表没有回滚,

contacts 回滚失败的分析

通过 sequel pro 连上数据库查看

发现 contacts 表果然存在,但是 migration 表中已没有内容,想必再执行前面 migrate 命令的时候出现错误,contacts 的执行记录并没有写入 migrations 表中。我们可以重新执行 migrate 命令试试看。首先手动删除这两张表,也就是清空数据库,然后执行:

  1. php artisan migrate 

我们先忽视创建 contacts 表出现的错误,刷新 sequel pro 查看一下:

果然,migration 表中没有 contacts 的创建记录,这也就难怪执行 reset 的时候,会没有 contacts 的回滚操作了。

contacts 无法创建 branch_id 外键的解决

现在,我们已经执行了 migrate 命令,我们重新来看看这个最早出现的错误:

  1. [Illuminate\Database\QueryException] 
  2.  
  3. SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `contacts` add constraint `contacts_branch_id_foreign` foreign key (`branch_id`) references `br 
  4.  
  5. anches` (`id`) on update cascade
  6.  
  7. [PDOException] 
  8.  
  9. SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint  

冷静下来分析一下,既然提示的是 SQL 错误,我们不妨在 sequel pro 中手工执行一下这条 SQL 语句。 

果然,执行返回错误。

仔细查看语句并没有错误,一想,应该是 branch_id 类型声明和 branches 表中的 ID 类型不一致造成的吧。查看 contacts 的结构,发现 Unsigned 没有打钩,勾选后再执行增加外键的 SQL 语句,成功。

找到问题原因后,我们就清空数据库,修改 contacts 的 migration 文件,调整 branch_id 为: 

  1. $table->integer('branch_id')->unsigned()->comment('机构ID'); 

再重新执行 migrate 命令,成功!

 

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2020-11-05 18:53:15

JavaScript开发前端

2017-04-07 09:02:06

Spark方法优化

2010-04-19 14:33:06

Oracle tns配

2011-05-18 14:44:13

mysql1046错误

2013-05-30 14:21:38

2009-12-30 15:29:14

2011-06-08 09:37:19

C#Equals

2011-05-16 09:54:22

mysql1067错误

2022-10-19 23:18:27

KubernetesPod错误

2015-12-21 11:45:27

C语言常见问题错误

2010-01-22 15:42:01

VB.NET错误

2009-12-18 11:03:45

Ruby watir环

2013-08-13 13:38:13

Android错误解决

2010-05-26 13:14:22

MySQL错误解决方案

2010-08-11 10:41:22

AdobeFlex

2010-07-27 11:19:18

Flex

2019-10-09 13:39:39

Python编程语言异常错误

2010-01-18 14:41:33

VB.NET抓取网页

2010-07-08 14:24:21

SQL Server

2010-02-24 16:30:52

WCF常见错误
点赞
收藏

51CTO技术栈公众号