php - Laravel Eloquent create method causing integrity constraint violation -
i've got following eloquent models , relations set up:
applicant
class applicant { public function application() { return $this->hasone(application::class); } }
application
class application { public function applicant() { return $this->belongsto(applicant::class); } protected $fillable = [ // ... other fields 'applicant_id', ]; }
and migrations:
applicants
table
public function up() { schema::create('applicants', function (blueprint $table) { $table->increments('id'); // other fields ... }); }
applications
table
public function up() { schema::create('applications', function (blueprint $table) { $table->increments('id'); // other fields ... $table->integer('applicant_id')->unsigned(); // other relations ... $table->foreign('applicant_id')->references('id')->on('applications')->ondelete('cascade'); }); }
i want create new applicant add new application applicant created:
$applicant = applicant::create([ // data ]); $application = $applicant->applications()->create([ // data ]);
however, when running code receive following error:
sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails ( `dbname`.`applications`, constraint `applications_applicant_id_foreign` foreign key (`applicant_id`) references `applications` (`id`) on delete cascade ) (sql: insert `applications` ( `status`, `reference_number`, `organisation_id`, `qualification_id`, `applicant_id`, `updated_at`, `created_at` ) values ( pending, 573066ce59bfe, 24, 1, 12, // <------ applicant_id, i.e. foreign key! 2016-05-09 11:30:38, 2016-05-09 11:30:38 ))
as understand, error occuring because i'm passing foreign key doesn't exist in database. wondering happening here first call db make create applicant:
$applicant = applicant::create([ // data ]);
is not written db before second statement creates application:
$application = $applicant->application()->create([ // data ]);
... , causing integrity constraint violation? because if dd($applicant)
after creating or dd(applicant::find($applicant->id))
record , can view in db.
i've found prior re-factoring code when calling create()
add new record building new application line line , using save()
, didn't encounter issue. i'm curious know how both calls differ (if do) , correct way approach such scenario thought simple eloquent i've done many times before. advice appreciated, thanks!
edit 1
corrected call relation:
$applicant->application()->create(...)
not:
$applicant->applications()->create(...)
edit 2
running code:
$applicant = applicant::create($this->applicantdatafromrequest($request)); $application = new application([ 'status' => 'pending', 'reference_number' => $request->get('passport_number'), 'organisation_id' => $request->get('organisation'), 'qualification_id' => $request->get('qualification') ?: null, ]); $applicant->application()->save($application); dd('done');
and receiving same error (updated errors on page):
sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails (`dbname`.`applications`, constraint `applications_applicant_id_foreign` foreign key (`applicant_id`) references `applications` (`id`) on delete cascade) (sql: insert `applications` (`status`, `reference_number`, `organisation_id`, `qualification_id`, `applicant_id`, `updated_at`, `created_at`) values (pending, 5730970933c7b, 24, 1, 22, 2016-05-09 14:56:25, 2016-05-09 14:56:25)) sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails (`dbname`.`applications`, constraint `applications_applicant_id_foreign` foreign key (`applicant_id`) references `applications` (`id`) on delete cascade)
edit 3 added relevant migrations
update 1
really strange! i've reverted older version of project via git working before began re-factoring code , that's not working same reason! no clue what's going on here!?
update 2
just noticed silly in migrations:
applications
table
public function up() { schema::create('applications', function (blueprint $table) { $table->increments('id'); // other fields ... $table->integer('applicant_id')->unsigned(); // other relations ... $table->foreign('applicant_id')->references('id')->on('applications')->ondelete('cascade'); }); }
the foreign key should be:
$table->foreign('applicant_id') ->references('id') ->on('applicants') ->ondelete('cascade'); // not applications!
two points here:
why did not effect code til now? it's been working fine til weird...
going update foreign key constraint , try out see if works now
try this:
$applicant = new applicant([ // data ]); $application = new application([ // data ]); $applicant->applications()->save($application);
Comments
Post a Comment