mysql - Attaching pivots using Laravel model on a different connection -


i trying copy data 1 database (both share same schema). both databases share same host.

in config/database.php have following:

/* |-------------------------------------------------------------------------- | default database connection name |-------------------------------------------------------------------------- | | here may specify of database connections below wish | use default connection database work. of course | may use many connections @ once using database library. | */  'default' => 'connection1',  /* |-------------------------------------------------------------------------- | database connections |-------------------------------------------------------------------------- | | here each of database connections setup application. | of course, examples of configuring each database platform | supported laravel shown below make development simple. | | | database work in laravel done through php pdo facilities | make sure have driver particular database of | choice installed on machine before begin development. | */  'connections' => [     'connection1' => [         'driver'    => 'mysql',         'host'      => 'host.link.here'         'database'  => 'db1',         'username'  => env('db_username'),         'password'  => env('db_password')     ],      'connection2' => [         'driver'    => 'mysql',         'host'      => 'host.link.here',         'database'  => 'db2',         'username'  => env('db_username_2'),         'password'  => env('db_password_2')     ], ], 

i wrote command duplicate experiment db1 db2. relevant part of code here:

$experiment = app\experiment::where('key', 1234)->first(); $connection = 'connection2';  db::connection($connection)->transaction(function() use ($experiment, $connection) {         $clone_experiment = $experiment->replicate();         $clone_experiment->setconnection($connection);         $clone_experiment->save();          $this->info('created experiment on destination db id ' . $clone_experiment->id);          // copy objectives         foreach ($experiment->objectives $objective) {             $clone_objective = $objective->replicate();             $clone_objective->setconnection($connection);             $clone_objective->save();              // attach objective experiment pivot data             $pivot_data = [                 'field_1' => $objective->pivot->field_1,                 'field_2' => $objective->pivot->field_2,             ];              $clone_experiment->objectives()->attach([$clone_objective->id => $pivot_data]);         }         $this->info('duplicated objectives'); }); 

there seems issue attaching pivot model on db2. output when running command:

created experiment on destination db id 3626 exception: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails (`db1`.`experiment_objective`, constraint `experiment_objective_experiment_id_foreign` foreign key (`experiment_id`) references `experiments` (`id`) on delete cascade) (sql: insert `experiment_objective` (`created_at`, `experiment_id`, `objective_id`, `field_1`, `field_2`, `updated_at`) values (2017-07-25 23:54:25, 3626, 4973, 10, exploration, 2017-07-25 23:54:25)) 

the error indicates constraint db1 failing, not know why looking @ since both $clone_experiment , $clone_objective using connection2, connects db2. there way around this?


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -