2015-10-17 182 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

相关问题