简体   繁体   中英

Laravel model is created with id = 0

I am having problem with eloquent. Here's some code:

$BaseTask = $TaskClass::create([
        'user_id' => $User->id,
        'task_type' => $this->task_type,
        'name' => $this->settings['name'],
        'task_' . $this->social_network . '_list_id' => $task_list_id,
]);
$BaseTask->accounts()->attach($account_id);

The problem is that SOMETIMES the last line fails with a foreign key constraint fails - it tries to update pivot table with query

insert into pivot ('account_id', 'task_id') values (289530, 0))

So somehow:

$BaseTask->id=0;

but i have property:

$incrementing=true;

Maybe someone faced such issue, any help appreciated!

I have experienced the same issue, you need to put:

public $incrementing = false;

on the model.

use save() method instead, since you don't have many to many relationship. If you do have many to many use sync() function then. Also go to database.php file set mysql strict to false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM