I have recently stumbled on a strange problem - maybe somebody has seen it / knows how to deal with it?

I have a many-to-many relationship (users and lessons), and a pivot table with custom columns that links user with a lesson, and stores tracking data (e.g. how much time spent in a lesson). When I started to write unit tests, I discovered that whenver I try to execute (for simplicity of explanation) twice, consecutively something like:

$user->lessons()->sync([$lesson_id, [custom data]]);
$user->lessons()->sync([$lesson_id, [custom data]);

... the second call fails with a message like:

 Integrity constraint violation: 19 UNIQUE constraint failed: users_lessons.user_id, users_lessons.lesson_id (SQL: insert into "users_lessons" ("lesson_id", [...] "user_id") values ([...]))

That is, the 2nd call to sync() is attempting to execute insert() with the same user_id and lesson_id, rather than update the record. The same happens with SyncWithoutDetaching(), and ... I have a feeling that in laravel 5.x it was actually working ok.

Is there some defrred execution or caching system in action that causes the 2nd call to sync() fail to note that there was another one executed just couple of lines above? I'm 8 hours into trying to find a solution on my own, and have no idea where else to look...


If you convert your "feeling" with an actual test with 1.0.x branch, we may be inclined to check where the problem is... ;)


Haha ;-) I got what I asked for - I shouldn't be using such "improper" words.

Nevertheless, from what you just wrote, I feel that your answer to my actual question of "should two consecutive sync() calls fail" is a "no", and so I will gladly prepare a proper test case later today ;-)

Thanks again, Grze


Hi @mjauvin

Here is an isolated test case: https://github.com/cjkpl/oc-sync-plugin

I have used two simplest tables + a pivot, and there is a single unit test that works on 1.0, and fails on dev.

I will appreciate any help with this.


Last updated

1-4 of 4