12
Views
3
Comments
Solved
Cannot add or update a child row: a foreign key constraint fails
Question

Hi guys,
I'm new here, I'm still learning.

I'm developing a personal project just to learn, practice and get used to OutSystems.
So, here's my problem:

I've a form that when it is submitted I want to save it's data in two different entities, a main one with some general data (working fine apparently) and one secondary with a foreign key relative to the previous one.

I'm getting this error:

Cannot add or update a child row: a foreign key constraint fails (#######.`tb_links`, CONSTRAINT `tb_links_ibfk_1` FOREIGN KEY (`banda_id`) REFERENCES `tb_bandas` (`banda_id`))

I bet it's because of the foreign key, so I need to get the new created record banda_id to the tb_links.... but I'm not getting it...
Can someone please give me some lights on how to do it?

Capturar.JPG

Rank: #487
Solution

Hi João,


How are you setting the banda_id in your "tb_links" entity? 

You need to set the Id of the "tb_bandas" record you just created. The Create or CreateOrUpdate entity actions have an output that is an Id, and it's that Id that you need to set on your foreign key.


Hope this helps,

Pedro

Rank: #3101

I was so close to get to that! I was failing by a detail but your answer guided me to it!
Many thanks!
Loving OutSystems!

Rank: #487

Made that one a lot of times myself!

Keep going!