Support the ongoing development of Laravel.io →
Database Eloquent

Hi, this is my first thread so if I doing wrong some things please don't hate me :) I'm using Migration to build my database and I have a problem with ONE foreign key! My database contain 5 foreign key but I don't know why only one doesn't work. Full code: http://pastebin.com/4XRD1TuG

The problem is in Users <- Levels (N <- 1) If I try to add a new users laravel returns me this error: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (squarenews2.users, CONSTRAINT users_level_id_foreign FOREIGN KEY (level_id) REFERENCES levels (id)) If I try with phpmyadmin the user is placed but foreign key is null.... Where am I wrong? Thankyou :)

Last updated 3 years ago.
0

First the good news: we don't hate you! :D

I've had this error a few times and it's annoying. It's either that the FK is not set in the query, or the FK does not exist. Take a look at your code to make sure that everything is set OK.

Mei

Last updated 3 years ago.
0

meigwilym said:

First the good news: we don't hate you! :D

I've had this error a few times and it's annoying. It's either that the FK is not set in the query, or the FK does not exist. Take a look at your code to make sure that everything is set OK.

Mei

When I try with phpmyadmin, I use che dropdown menu to select the FK... So the FK exists! Migration code is all right? (Ps: if I remove foreign key , the query run without problems!)

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

AsoStrife asostrife Joined 9 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.