Home PHP Framework Laravel Summary of some common errors in laravel migrate

Summary of some common errors in laravel migrate

Jan 05, 2022 pm 04:07 PM
laravel migrate

The following tutorial column of Laravel will introduce you to some common mistakes and solutions for beginners of laravel migrate. I hope it will be helpful to everyone!

Preface

I have recently started to learn laravel intermittently. I want to create a simple address book system and set up two tables, one for branches and one for contacts. When creating the migration file, I didn't think carefully and first created the contacts table. The contacts table has a foreign key connected to the branches' IDs. As a result, when executing the migrate command, the following error occurred:

[Illuminate\Database\QueryException] 
 
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `contacts` add constraint `contac 
 
ts_branch_id_foreign` foreign key (`branch_id`) references `branches` (`id`) on delete cascade) 
 
[PDOException] 
 
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Copy after login

Initial suspicion It was caused by irregular table creation. Therefore, I manually modified the date on the migration file name of branches, and then executed

php artisan migrate:reset
Copy after login
Copy after login

and the following error occurred:

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

failed to open Stream error resolution

It’s not very understandable just by looking at the error message. Let’s check laravel’s log file

more storage/logs/laravel.log
Copy after login

to find the paragraph where ERROR appears:

[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 
Stack trace: 
#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) 
#1 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(412): Composer\Autoload\includeFile() 
#2 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(301): Composer\Autoload\includeFile('/Users/Ade/www/...') 
#3 [internal function]: Composer\Autoload\ClassLoader->loadClass('CreateBranchesT...') 
#4 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(335): spl_autoload_call('CreateBranchesT...') 
#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...') 
#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)
Copy after login

The error appeared in line 412 of the ClassLoader.php file

Looking at the changed code, it was found to be a statement calling a file:

Summary of some common errors in laravel migrate

This file has been pointed out in the log file, namely resolve('2016_09_12_1728...') . The name prompted by the log is the name of the migration file of the branch I modified.

Let’s search where normal migration files appear:

mdfind 2014_10_12_000000_create_users_table.php|grep phonebook
Copy after login

Summary of some common errors in laravel migrate

It can be seen that there are 3 places where normal migration files appear. Modify Only appeared in 1 place.

Edit these two files that do not appear

Adjust the autoload_static.php file

I found that in the vendor/composer/autoload_static.php file, the statements related to branches are as follows:

'CreateBranchesTable' => __DIR__ .,
Copy after login

I think it was when I changed the name, PHP Storm automatically helped me delete all the branches file paths in this file. Lost. Just add it back.
Refer to the configuration of the normal migration file name, add the following

'CreateBranchesTable' => __DIR__ . '/../..' . '/database/migrations/2016_09_12_172822_create_branches_table.php',
Copy after login

Adjust the autoload_classmap.php file

We found In the autoload_classmap.php file, the path name of branches is still the path before modification:

'CreateBranchesTable' => $baseDir . '/database/migrations/2016_09_29_172822_create_branches_table.php',
Copy after login

Modify it to

'CreateBranchesTable' => $baseDir . '/database/migrations/2016_09_12_172822_create_branches_table.php',
Copy after login

and then execute the migrate command

php artisan migrate:reset
Copy after login
Copy after login

Summary of some common errors in laravel migrate

OK, the error just now is gone, but we found that the contacts table was not rolled back,

Analysis of failed rollback of contacts

Connect to the database through sequel pro to view

Summary of some common errors in laravel migrate

It is found that the contacts table does exist, but there is no content in the migration table. I must execute the previous migrate again An error occurred during the command, and the execution records of contacts were not written to the migrations table. We can re-execute the migrate command and give it a try. First manually delete these two tables, that is, clear the database, and then execute:

php artisan migrate
Copy after login

Let’s ignore the error in creating the contacts table, refresh sequel pro and check:

Summary of some common errors in laravel migrate

Sure enough, there is no creation record of contacts in the migration table, which is why there is no rollback operation of contacts when executing reset.

contacts Unable to create branch_id foreign key solution

Now that we have executed the migrate command, let’s start again Take a look at this earliest error:

[Illuminate\Database\QueryException] 
 
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 
 
anches` (`id`) on update cascade) 
 
[PDOException] 
 
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Copy after login

Calm down and analyze it. Since the prompt is a SQL error, we might as well execute this SQL statement manually in sequel pro.

Summary of some common errors in laravel migrate

Sure enough, the execution returned an error.

Check the statement carefully and there is no error. At first thought, it should be caused by the inconsistency between the branch_id type declaration and the ID type in the branches table. Check the structure of contacts and find that Unsigned is not checked. After checking it, execute the SQL statement to add the foreign key and it succeeds.

Summary of some common errors in laravel migrate

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

$table->integer('branch_id')->unsigned()->comment('机构ID');
Copy after login

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

Summary of some common errors in laravel migrate

相关推荐:最新的五个Laravel视频教程

The above is the detailed content of Summary of some common errors in laravel migrate. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Comparison of the latest versions of Laravel and CodeIgniter Comparison of the latest versions of Laravel and CodeIgniter Jun 05, 2024 pm 05:29 PM

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

How do the data processing capabilities in Laravel and CodeIgniter compare? How do the data processing capabilities in Laravel and CodeIgniter compare? Jun 01, 2024 pm 01:34 PM

Compare the data processing capabilities of Laravel and CodeIgniter: ORM: Laravel uses EloquentORM, which provides class-object relational mapping, while CodeIgniter uses ActiveRecord to represent the database model as a subclass of PHP classes. Query builder: Laravel has a flexible chained query API, while CodeIgniter’s query builder is simpler and array-based. Data validation: Laravel provides a Validator class that supports custom validation rules, while CodeIgniter has less built-in validation functions and requires manual coding of custom rules. Practical case: User registration example shows Lar

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Which one is more beginner-friendly, Laravel or CodeIgniter? Which one is more beginner-friendly, Laravel or CodeIgniter? Jun 05, 2024 pm 07:50 PM

For beginners, CodeIgniter has a gentler learning curve and fewer features, but covers basic needs. Laravel offers a wider feature set but has a slightly steeper learning curve. In terms of performance, both Laravel and CodeIgniter perform well. Laravel has more extensive documentation and active community support, while CodeIgniter is simpler, lightweight, and has strong security features. In the practical case of building a blogging application, Laravel's EloquentORM simplifies data manipulation, while CodeIgniter requires more manual configuration.

Laravel vs CodeIgniter: Which framework is better for large projects? Laravel vs CodeIgniter: Which framework is better for large projects? Jun 04, 2024 am 09:09 AM

When choosing a framework for large projects, Laravel and CodeIgniter each have their own advantages. Laravel is designed for enterprise-level applications, offering modular design, dependency injection, and a powerful feature set. CodeIgniter is a lightweight framework more suitable for small to medium-sized projects, emphasizing speed and ease of use. For large projects with complex requirements and a large number of users, Laravel's power and scalability are more suitable. For simple projects or situations with limited resources, CodeIgniter's lightweight and rapid development capabilities are more ideal.

Questions and Answers on PHP Enterprise Application Microservice Architecture Design Questions and Answers on PHP Enterprise Application Microservice Architecture Design May 07, 2024 am 09:36 AM

Microservice architecture uses PHP frameworks (such as Symfony and Laravel) to implement microservices and follows RESTful principles and standard data formats to design APIs. Microservices communicate via message queues, HTTP requests, or gRPC, and use tools such as Prometheus and ELKStack for monitoring and troubleshooting.

Laravel vs CodeIgniter: Which framework is better for small projects? Laravel vs CodeIgniter: Which framework is better for small projects? Jun 04, 2024 pm 05:29 PM

For small projects, Laravel is suitable for larger projects that require strong functionality and security. CodeIgniter is suitable for very small projects that require lightweight and ease of use.

Which is the better template engine, Laravel or CodeIgniter? Which is the better template engine, Laravel or CodeIgniter? Jun 03, 2024 am 11:30 AM

Comparing Laravel's Blade and CodeIgniter's Twig template engine, choose based on project needs and personal preferences: Blade is based on MVC syntax, which encourages good code organization and template inheritance. Twig is a third-party library that provides flexible syntax, powerful filters, extended support, and security sandboxing.

See all articles