-1

Ok Guys, This is absolutely a weird one. So, i am fetching game information data from IGDB, and i am saving it in my database Whenever the user click on it to view more information about the game.

Give more Details: I have an object model call "Game", it contain property such as name, rating, popularity, cover, screenshots, videos, etc..., among other navigation properties.

Game Model

And this is my Cover Model.. The reason why i am showing the cover model, is because it plays a role in the error...I think.. The cover model has a foreign key of gameId and other properties.

Cover model

So by now you know that there is a one to one relationship between 'Game' and 'Cover'. Meaning one game can have one cover.

Now the problem: When i click on "most" of the games, It work fine, the games get inserted and the covers also.
BUT BUT .. There is one game (as far as i know ) that when i click on it i get this error:

MySqlException: Cannot add or update a child row: a foreign key constraint fails (gamerater_db.covers, CONSTRAINT FK_Covers_Games_gameId FOREIGN KEY (gameId) REFERENCES games (gameid) ON DELETE CASCADE)

As for as i know this is the only one that gives that error.(the api database is quite big)

So you can see why this is weird... Most of the game is getting saved properly but this particular one is giving that error.. Keep in mind as for as i know this is the only one that is giving that error.

So, literally, i do not know where the error can be. When i debug log the game that is giving me that error before it get inserted, the gameId Is there, and the other properties such as name, rating, popularity, summary, user_id, first_release_date, etc... are also present.

This is the models before i insert them in the database

The saving of game model is working for the others but not for this particular one.. Why??? Everything is the same.. any insight would be useful..

Aaron Dasani
  • 73
  • 1
  • 1
  • 9
  • Possible duplicate of [ERROR 1452: Cannot add or update a child row: a foreign key constraint fails](https://stackoverflow.com/questions/21659691/error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails) Also try not to post code as images. – Jimenemex Jan 25 '19 at 18:58
  • Thanks for the advice :) .. i check the ids and foreign keys for both the cover and the game model before they get inserted and there is no table in the database that have the same ids and foreign keys. Yeah i can't seem to figure this out... the other games are getting save when i click of them, but just this one is not being save because of this error... – Aaron Dasani Jan 28 '19 at 07:41
  • is there a chance that your code is trying to insert the same thing twice in one session? The first beings successful and the second failing? – Jimenemex Jan 29 '19 at 16:19
  • Dude, That what happen...lol Thanks so much. So what happen was the Game Model had the Cover model already in it, so C# just automatically added the Cover Model to the database when I was trying to just add the Game Model to the database. And then when I was trying to add the Cover, it did not work because it was already inserted automatically when I added the Game Model to the database. That made me understand C# more.. :) Thanks.. – Aaron Dasani Jan 31 '19 at 06:00

1 Answers1

1

So what happen was the Game Model had the Cover model already in it, so C# just automatically added the Cover Model to the database when I was trying to just add the Game Model to the database. And then when I was trying to add the Cover, it did not work because it was already inserted automatically when I added the Game Model to the database. That made me understand C# more

Aaron Dasani
  • 73
  • 1
  • 1
  • 9