Illuminate \ Database \ QueryException SQLSTATE [42000]

时间:2018-10-03 09:26:43

标签: php mysql laravel

我正在使用Laravel,当我尝试运行 JSONObject jsonObject = new JSONObject(); jsonObject.put("value", "value1"); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("value2", "value2"); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("camp1", jsonObject); jsonObject2.put("camp2",jsonObject1); //jsonObject2 is the payload to server here you can use JsonObjectRequest String url="your custom url"; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest (Request.Method.POST,url, jsonObject2, new com.android.volley.Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { //TODO: Handle your response here } catch (Exception e){ e.printStackTrace(); } System.out.print(response); } }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO: Handle error error.printStackTrace(); } }); 时会显示此错误:

  

Illuminate \ Database \ QueryException:SQLSTATE [42000]:语法错误或访问冲突:1071指定的密钥太长;默认值为0。密钥最大长度为767字节(SQL:alter table php artisan make:migrate添加唯一的usersusers_email_unique))

     

在/Users/soksan/Desktop/TestLaravel/multiUsers/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664

email

异常跟踪:

  

1 660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) { > 664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668|
       /Users/soksan/Desktop/TestLaravel/multiUsers/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

     

2 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
       /Users/soksan/Desktop/TestLaravel/multiUsers/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

请使用参数PDOStatement::execute()查看更多详细信息。

3 个答案:

答案 0 :(得分:2)

进入App\Providers\AppServiceProvider类,并将启动方法更改为如下形式

public function boot()
{
    Schema::defaultStringLength(191);
}

答案 1 :(得分:1)

转到您的laravel项目,然后在该项目内打开App\Providers\AppServiceProvider类,并更改启动方法并添加新的架构,如图所示。

将此行添加到顶部

use Illuminate\Support\Facades\Schema;

替换此方法

public function boot() { Schema::defaultStringLength(191); }

Go to AppServiceProvider.php

答案 2 :(得分:0)

找到文件config / database.php并将字符集和排序规则从utf8mb4更改为utf8

'字符集'=>'utf8', 'collat​​ion'=>'utf8_unicode_ci',