MySQL error id 1452
#1

I was creating a vehicle system..i had a problem..they said i should create a foreign key..i did.Not i can not write anything into my table and this is the error im getting:
pawn Код:
[17:12:39] CMySQLHandler::Query(INSERT INTO vehicles (vehicleid,owner,model,x,y,z,r,LPlate,EngineHealth,CColor1,CColor2) VALUES (NULL,'Enrico_Mancini',411,-164.632904,1011.897399,19.617200,92.183296,'ZPF 606',1000,212,88)) - An error has occured. (Error ID: 1452, Cannot add or update a child row: a foreign key constraint fails (`bebras`.`vehicles`, CONSTRAINT `vehicles_ibfk_1` FOREIGN KEY (`vehicleid`) REFERENCES `users` (`userid`) ON DELETE CASCADE ON UPDATE CASCADE))
Reply
#2

You're doing it all wrong! A foreign key (or simply relation) links two pieces of the same data together, that are in different tables. Userid needs to be linked with userid, not vehicleid with userid. That will never work.

Refer to this picture; here I link the player's weapons (child table) to the player's main data (parent table) by means of the player's unique identifier. The point of doing this is: in the event the userid gets changed or deleted, all rows in the child table(s) will also get updated or deleted.

Reply
#3

Wait, you mean that userid has to be linked with itself? Or it just has to be called userid in both tables
Reply
#4

Yes, it needs to be linked to itself. Like I said in my previous post, if the user gets deleted then all the rows in the child table that relate to that user (hey, there's that word again!) will also get deleted. This prevents orphaned rows and preserves database integrity.
Reply
#5

In this case the thing i do not understand is,what good can it do if its linked to itself? It's just not logical
Reply
#6

I linked it anyway...
pawn Код:
An error has occured. (Error ID: 1452, Cannot add or update a child row: a foreign key constraint fails (`bebras`.`users`, CONSTRAINT `users_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`)))
Thats the result of it
Reply
#7

But isn't userid the parent? although it's linked to itself.This makes no sense
Reply
#8

bump
Reply
#9

anyone?
Reply
#10

bump
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)