create account

Laravel #6 - How to fix SQLSTATE error [42000]: Syntax error or access violation: 1071 Specified key was too long in laravel by iwaydi

View this thread on: hive.blogpeakd.comecency.com
· @iwaydi · (edited)
$20.17
Laravel #6 - How to fix SQLSTATE error [42000]: Syntax error or access violation: 1071 Specified key was too long in laravel
#### What Will I Learn?
<hr>

Errors are often encountered while running a program and are no exception in laravel and this is very disturbing and slowing down our work. So in this tutorial, we will try to fix errors that may occur in this project. Here's what you'll learn:

- you will learn to find the source of SQLSTATE errors [42000]
- you will learn to fix SQLSTATE error [42000]

#### Requirements
<hr>

To follow this tutorial. You only need to provide some tools. o this is the list of the requirements for the user:

- Xampp 5.6.3
- Framework Laravel 5.4
- Composer
- Atom text editor

#### Difficulty
<hr>

- Intermediate

#### Preliminary
<hr>

**Errors** are Error, Incorrect, or Malfunction. With the occurrence of this error, the execution of the command does not match what is expected. On some occurrence, if an error occurs, the current command who be working will be stopped because it can not continue, because the server is not running yet. But in other circumstances, the work can still continue, because the errors that occur do not affect the system and can be ignored. Just like our mistakes. Some commands have been executed but there are commands that can not be executed and errors occur.


**SQLSTATE error [42000]** is a warning that appears when there is an error in the process and this affects the existing database and 42000 is the number used to recognize the error that occurred and it is indicated for syntax errors or access violations. This error can occur because there are some incorrect syntax we make or not listed in the system and usually the error occurs because there are other errors as the trigger associated with the error displayed on the screen and you need to know the error on display is not the real problem in this case but it will be a clue to find the actual error.

#### Tutorial Content : Practice Fixing error SQLSTATE  [42000]
<hr>

In this tutorial, we will discuss how to fix the error and as usual, we will finish in 3 steps. We start from making the error, then look for the source of error, and fix the error.

##### Step 1: Making eror SQLSTATE  [42000] 
<hr>

The first, we will discuss the error below and we will start trying to make something and display the error.

> <code>SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))</code>

Now we will create the error as above and we will try to create a table so we can get the error it. First, create a migration file named **table_error**. So use the command below to create a migration file.

<code>php artisan make:migration table_error</code>

After that open the migration file and add this code:
```laravel
  public function up()
    {
        Schema::create('eror', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('No.Error',60);
            $table->varchar('name',60);
            $table->text('category');
            $table->text('information');
            $table->varchar('Description',60);
            $table->text('solution');
            $table->timestamps();
        });
    }
```
After that, use this script to execute migration files that have been edited

<code>php artisan migrate</code>

Here is an output  the command prompt after running:
![eror1.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518966696/uwpbbh1usolclbdk2bhw.png)

You can see the output above. It says Migration table created successfully. But why error?. Now we already have the error and in the next step we will find out why this can happen.

##### Step 2: Looking for the source of error SQLSTATE [42000] 
<hr>

Before looking for it. We look again the following output and you will find the **table user**. Why can the **table user** appear while we create **table_error**?
![eror2.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518967781/fvakkxygxppilocykcey.png)

After looking at it I assume if the error is in file migration and it turns out I am right. I'll show you how I found the error. Open the atomic text editor and then open the laravel folder into it. After that go to <code> database/migrations </code>.This is my migration file view and you can see there is **create_users_table** file. So I think this is the source of the problem and I delete that file and a new problem arises when I execute again using <code>php artisan merge</code>.

![eror3.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518968616/v40xg4xbl9htb5lwous1.png)

This is the output at the command prompt after I delete the **create_user** migration file and the results are similar to before but there are different there. Now appears <code> table `password_resets` </code>

![eror4.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969245/ekyovluehzma8o8nmrij.png)

So I think this is the same as the previous user. Because I see it also in the migration folder. So I delete it and now inside the migration folder there is only 1 migration file left and that is **table_eror**  but other errors appear but I think this is the source of the problem. This is the cause of the previous error. Although Migration table created successfully. This is the migration folder view now.

![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969743/te9rfawytwflxyru902n.png)

The following is the output at the command prompt after the migration file has one left in the folder and is executed

![eror5.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969847/ngihjqkimcwtg1tceaqs.png)

Now we have found the problem. it turns out the **varchar** method cannot be used in the migration file and this is the cause of all previous errors. migration user files that we have previously deleted have execution and because there is an error then the system does re-execute the user file while the table user was successfully created so that there was an error. well in the next step we will fix this error.

##### Step 3: Fixing error SQLSTATE  [42000] 
<hr>

Now open the table_error migration file and then replace it with the code below that I have fixed:

```laravel
    public function up()
    {
        Schema::create('eror', function (Blueprint $table) {
            $table->integer('eror_id');
            $table->increments('id');
            $table->string('nameeror',60);
            $table->text('category');
            $table->text('information');
            $table->string('Description',60);
            $table->text('solution');
            $table->timestamps();
        });
    }
```

After that, use this script to execute migration files that have been edited

<code>php artisan migrate</code>

Here is an output from the command prompt after running:
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518971591/kqvgycdpsnpahskk2oxu.png)

I replace the **varchar** method into the **string<**and then the object **No.Error** becomes **eror_id** and I put it in the first position. then I change the object **name** to **name error** and the results do not appear again error and the table successfully created and now we see the column of the table and you will find interesting facts.

![eror5.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518972432/tg3eugzl2jjoojtrusly.png)

From the table structure above we can see. All **string** methods change to **varchar** after the file migration in execution. this is what I want to show you. File migration can not use **varchar** but you can use **string** for **varchar**method because it will change after going into the database and remember this to avoid this kind of error again.

#### Conclusion
<hr>

SQLSTATE error [42000] can happen because there is an error in the migration file we created so that the system will automatically execute the migration file on it that has already been executed previously so this error can occasion. In our case, the error we made is using the **varchar** method and the solution by replacing the **varchar** into a **string**.

#### Curriculum
<hr>

- [Laravel #5 - How to Use Rename table with schema in laravel](https://utopian.io/u/33254961)
- [Laravel #4 - How to Use Feature Dropping and Rollback with Migration in Laravel](https://utopian.io/u/32624333)
- [Laravel #3 - How to Use Maintenance Mode with composer in Laravel](https://utopian.io/u/31714784)
- [Laravel #2 - How to Modifications a table database with PHP artisan migration in laravel](https://utopian.io/u/31071228)
- [Laravel #1 - How to Create a Table Database in Laravel With PHP Artisan Migration](https://utopian.io/u/30328342)
    

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@iwaydi/laravel-6-how-to-fix-sqlstate-error-42000-syntax-error-or-access-violation-1071-specified-key-was-too-long-in-laravel">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , ,
properties (23)
authoriwaydi
permlinklaravel-6-how-to-fix-sqlstate-error-42000-syntax-error-or-access-violation-1071-specified-key-was-too-long-in-laravel
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":1863329,"name":"laravel","full_name":"laravel/laravel","html_url":"https://github.com/laravel/laravel","fork":false,"owner":{"login":"laravel"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","laravel","tutorial","contribution","programming"],"users":["iwaydi"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1518966696/uwpbbh1usolclbdk2bhw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518967781/fvakkxygxppilocykcey.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518968616/v40xg4xbl9htb5lwous1.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969245/ekyovluehzma8o8nmrij.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969743/te9rfawytwflxyru902n.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969847/ngihjqkimcwtg1tceaqs.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518971591/kqvgycdpsnpahskk2oxu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518972432/tg3eugzl2jjoojtrusly.png","https://utopian.io/u/33254961","https://utopian.io/u/32624333","https://utopian.io/u/31714784","https://utopian.io/u/31071228","https://utopian.io/u/30328342"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1518966696/uwpbbh1usolclbdk2bhw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518967781/fvakkxygxppilocykcey.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518968616/v40xg4xbl9htb5lwous1.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969245/ekyovluehzma8o8nmrij.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969743/te9rfawytwflxyru902n.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518969847/ngihjqkimcwtg1tceaqs.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518971591/kqvgycdpsnpahskk2oxu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1518972432/tg3eugzl2jjoojtrusly.png"],"moderator":{"account":"yokunjon","time":"2018-02-19T08:24:19.184Z","reviewed":true,"pending":false,"flagged":false},"questions":null,"score":null}
created2018-02-18 17:00:36
last_update2018-02-19 18:03:24
depth0
children3
last_payout2018-02-25 17:00:36
cashout_time1969-12-31 23:59:59
total_payout_value14.160 HBD
curator_payout_value6.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,922
author_reputation611,028,158,863
root_title"Laravel #6 - How to fix SQLSTATE error [42000]: Syntax error or access violation: 1071 Specified key was too long in laravel"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,562,647
net_rshares4,427,073,888,033
author_curate_reward""
vote details (9)
@utopian-io ·
### Hey @iwaydi I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-iwaydi-laravel-6-how-to-fix-sqlstate-error-42000-syntax-error-or-access-violation-1071-specified-key-was-too-long-in-laravel-20180219t171024548z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-19 17:10:27
last_update2018-02-19 17:10:27
depth1
children0
last_payout2018-02-26 17:10:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,504
author_reputation152,955,367,999,756
root_title"Laravel #6 - How to fix SQLSTATE error [42000]: Syntax error or access violation: 1071 Specified key was too long in laravel"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,838,764
net_rshares0
@yokunjon ·
Thank you for the contribution. It has been approved.

* I did approve, but you should fix your grammar. Otherwise, it won't be formal as stated in rules. Reason for my approval is your post showed how to reproduce it, detect it, fix it. And this is the preferable way of teaching something. If you need assistance with grammar, you can use Grammarly or like programs.

You can contact us on [Discord](https://discord.gg/uTyJkNm).

**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authoryokunjon
permlinkre-iwaydi-laravel-6-how-to-fix-sqlstate-error-42000-syntax-error-or-access-violation-1071-specified-key-was-too-long-in-laravel-20180219t082932607z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-19 08:31:00
last_update2018-02-19 08:31:00
depth1
children1
last_payout2018-02-26 08:31:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length488
author_reputation19,266,807,595,513
root_title"Laravel #6 - How to fix SQLSTATE error [42000]: Syntax error or access violation: 1071 Specified key was too long in laravel"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,732,643
net_rshares0
@iwaydi ·
Thanks mod @yokunjon I will fix my grammar. I will use Grammarly like your suggestion.😀
properties (22)
authoriwaydi
permlinkre-yokunjon-re-iwaydi-laravel-6-how-to-fix-sqlstate-error-42000-syntax-error-or-access-violation-1071-specified-key-was-too-long-in-laravel-20180219t105817227z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["yokunjon"],"app":"steemit/0.1"}
created2018-02-19 10:58:21
last_update2018-02-19 10:58:21
depth2
children0
last_payout2018-02-26 10:58:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length87
author_reputation611,028,158,863
root_title"Laravel #6 - How to fix SQLSTATE error [42000]: Syntax error or access violation: 1071 Specified key was too long in laravel"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,760,409
net_rshares0