2015-10-17 178 views
0

我正在使用自定義的API請求到我的數據庫以生成新條目。SQLState 23000 - 完整性約束違規1062重複條目

我的表結構是這樣的:

表結構

Schema::create('incidents', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('incident_id')->unique(); 
     $table->integer('incident_type')->unsigned(); 
     $table->string('location'); 
     $table->string('street'); 
     $table->string('city'); 
     $table->double('latitude', 10, 6); 
     $table->double('longitude', 10, 6); 
     $table->date('date'); 
     $table->time('time'); 
     $table->smallInteger('incident_archived')->default(0); 
     $table->timestamps(); 
    }); 

我的Incident_Type設置獨一無二的,因爲這是我的系統的要求。當我發佈新系統時:

SERVER_IP/v1/incidents?incident_id=1&city=Muenchen&street=Fichtenstr.20&latitude=100&longitude=300 

第一次工作正常。

第二次:

SERVER_IP/v1/incidents?incident_id=2&city=Muenchen&street=Fichtenstr.20&latitude=100&longitude=300 

當我使用不同的incident_id我得到的錯誤:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'incidents_incident_id_unique' (SQL: insert into `incidents` (`street`, `city`, `latitude`, `longitude`, `updated_at`, `created_at`) values (Fichtenstr.20, Muenchen, 100, 300, 2015-10-17 12:28:11, 2015-10-17 12:28:11)) 

爲什麼發送使用,即使我改變的數據完全相同的登記請求?我該如何解決這個問題?

回答

3

這是因爲您沒有爲incident_id傳遞任何值,所以它每次都默認爲空字符串。

insert into `incidents` (`street`, `city`, `latitude`, `longitude`, `updated_at`, `created_at`) values (Fichtenstr.20, Muenchen, 100, 300, 2015-10-17 12:28:11, 2015-10-17 12:28:11)) 

不是Laravel用戶爲什麼它不使用您在傳遞incident_id我不能告訴你。

+0

,幫助我了!我沒有看到...謝謝! – sesc360

相關問題