Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eddy1992's avatar

Facing error : SQLSTATE[23000]: Integrity constraint violation: 1452

Hi I am trying to save data to the table but fail as I am facing an error while executing save() and I am creating one to many relationship.

Relationship defined in Blog model :

in User model:

users table:

blogs table:

blogs controller:

Please tell me whats wrong I have no idea .

Thank You.

0 likes
5 replies
mstnorris's avatar

What order are your migrations run in? Do you have the relevant rows in your database? Also, you have pasted your create_blogs_table migration twice instead of your BlogController.

eddy1992's avatar

When I run php artisan migrate:refresh this is the order

mstnorris's avatar
Level 55

It means you don't have any users in your database. You need to insert a user before you can reference them.

Add say 10 users first, then try to add blog posts, you'll see that it works :)

1 like
eddy1992's avatar

Oh sorry I did not realize that there were no user created. Thank you so much @mstnorris

1 like
mstnorris's avatar

@eddy1992 it is an easy mistake to make, but, now you know what that error usually means so you won't make that mistake again :)

Please or to participate in this conversation.