Laravel 5: Integrity constraint violation: 1062 - Many to Many

user3641381

I need some advice on the following.

I have 2 migrations, like so.

        Schema::create('plates', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('serial_number');
        $table->string('crc-code', 50);
        $table->string('reason', 50)->nullable();

        $table->softDeletes();

        $table->timestamps();
    });

And another one

  Schema::create('documents', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 25)->unique();
        $table->text('description')->nullable();
        $table->longText('relative_path');

A pivot table for many to many relations I have setup just like this

        Schema::create('document_plate', function (Blueprint $table) {
      $table->integer('plate_id')->unsigned()->index();
      $table->integer('document_id')->unsigned()->index();

      $table->primary(['plate_id', 'document_id']);

      $table->timestamps();
    });

When taking a certain action I use the code below to attach a plate to a document

       $plate  =   Plate::find(1);
       $doc    =   Document::find(1);
       if($plate && $doc) {
          $plate->documents()->attach($doc->id);
       }

The first time, everything works just fine! the document_plate gets updated. The error occurs when having the same ids doing it again.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' (SQL: insert into document_plate (created_at, document_id, plate_id, updated_at)

Now the question

Is there a way to avoid getting errors and just update the table with the same ids??

Or.. I need to setup a some sort of validation on the front-end which tells the user (before submitting) that he/she choose the same id's which are already in the table.

Note: I am using AngularJS for front-end operations.

Thomas Kim

Laravel makes this very simple to achieve by using the sync method. The sync method by default will detach any ids that you don't pass to it, but it accepts a second argument that can disable the detaching.

$plate->documents()->sync([$doc->id], false);

This will only add the entry if it does not already exist in the table.

Link to the API: http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Relations/BelongsToMany.html#method_sync

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel integrity constraint violation when saving many to many relationship

From Dev

Laravel - Integrity constraint violation: 1062 Duplicate entry

From Dev

Hibernate Many to Many integrity constraint violation

From Dev

Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

Laravel 5: SQLSTATE[23000]: Integrity constraint violation

From Dev

Laravel : Integrity constraint violation

From Dev

Integrity constraint violation in laravel

From Dev

Integrity constraint violation in laravel

From Dev

laravel Integrity constraint violation

From Dev

Integrity constraint violation: 1062 Duplicate field

From Dev

Doctrine: Exception in One-To-Many cascading: Integrity constraint violation 1451

From Dev

Laravel - Integrity contraint violation: 1062 Duplicate entry

From Dev

Integrity constraint violation: 1452 laravel

From Dev

Laravel validator - Integrity constraint violation

From Dev

Integrity constraint violation: 1452 laravel

From Dev

Laravel validator - Integrity constraint violation

From Dev

Referential Integrity Constraint Violation when saving Many to Many - Ebean, Play, Yaml, Tests

From Dev

"Integrity constraint violation: 1062 Duplicate entry" - but no duplicate rows

From Dev

Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

From Dev

Magento - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

SQLState 23000 - Integrity constraint violation 1062 Duplicate Entry

From Dev

Doctrine One-To-Many Relationship Won't Save - Integrity Constraint Violation

From Dev

Doctrine One-To-Many Relationship Won't Save - Integrity Constraint Violation

From Dev

Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel 5: SQLSTATE[23000]: Integrity constraint violation, foreign key constraint fails

From Dev

Integrity constraint violation - PHP Laravel MVC 5.0

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row (laravel5)

From Dev

Save model in Yii twice - getting "Integrity constraint violation: 1062 Duplicate entry"

Related Related

  1. 1

    Laravel integrity constraint violation when saving many to many relationship

  2. 2

    Laravel - Integrity constraint violation: 1062 Duplicate entry

  3. 3

    Hibernate Many to Many integrity constraint violation

  4. 4

    Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  5. 5

    Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  6. 6

    Laravel 5: SQLSTATE[23000]: Integrity constraint violation

  7. 7

    Laravel : Integrity constraint violation

  8. 8

    Integrity constraint violation in laravel

  9. 9

    Integrity constraint violation in laravel

  10. 10

    laravel Integrity constraint violation

  11. 11

    Integrity constraint violation: 1062 Duplicate field

  12. 12

    Doctrine: Exception in One-To-Many cascading: Integrity constraint violation 1451

  13. 13

    Laravel - Integrity contraint violation: 1062 Duplicate entry

  14. 14

    Integrity constraint violation: 1452 laravel

  15. 15

    Laravel validator - Integrity constraint violation

  16. 16

    Integrity constraint violation: 1452 laravel

  17. 17

    Laravel validator - Integrity constraint violation

  18. 18

    Referential Integrity Constraint Violation when saving Many to Many - Ebean, Play, Yaml, Tests

  19. 19

    "Integrity constraint violation: 1062 Duplicate entry" - but no duplicate rows

  20. 20

    Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

  21. 21

    Magento - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  22. 22

    SQLState 23000 - Integrity constraint violation 1062 Duplicate Entry

  23. 23

    Doctrine One-To-Many Relationship Won't Save - Integrity Constraint Violation

  24. 24

    Doctrine One-To-Many Relationship Won't Save - Integrity Constraint Violation

  25. 25

    Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  26. 26

    Laravel 5: SQLSTATE[23000]: Integrity constraint violation, foreign key constraint fails

  27. 27

    Integrity constraint violation - PHP Laravel MVC 5.0

  28. 28

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row (laravel5)

  29. 29

    Save model in Yii twice - getting "Integrity constraint violation: 1062 Duplicate entry"

HotTag

Archive